[ create a new paste ] login | about

Link: http://codepad.org/rOhE1AqY    [ raw code | output | fork ]

C++, pasted on Mar 20:
#include <SDL.h>
#include <stdio.h> //use for things like printf, same as cout
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

//screend dimensions& sprtie dimensions
const int SCREEN_HEIGHT = 600;
const int SCREEN_WIDTH = 400;
const int SPRITE_WIDTH = 60;
const int SPRITE_HEIGHT = 80;

const int MAX_BULLETS = 50;

SDL_Window* Window = NULL;//the window rendering to
SDL_Surface* ScreenSurface = NULL;//surface contained by window
SDL_Surface* Background = NULL;
SDL_Surface* Player = NULL;
SDL_Surface* Enemy = NULL;
SDL_Surface* Bullet = NULL;
SDL_Surface* newBullet = NULL;

SDL_Rect posPlayer, posEnemy, posBullet, posnewBullet;

const Uint8* keystate = SDL_GetKeyboardState(NULL);
SDL_Event event;


class thePlayer
{
	public:
	thePlayer();
	
	void player_movement();
	void show_player();

	private:


};

class theBullet
{
	public:
	theBullet();

	bool isActive;
	int x_position;
	int y_position;

	void bullet_movement();
	void add_new_bullet();
	void show_bullet();


	private:

};
theBullet arrayofBullets[MAX_BULLETS];

class theEnemy
{
	public:
	theEnemy();

	void enemy_movement();
	void show_enemy();


	private:
};


thePlayer::thePlayer()
{
	posPlayer.x = 170;
	posPlayer.y = SCREEN_HEIGHT;
	posPlayer.w = 20;
	posPlayer.h = 30;

}

void thePlayer::player_movement()
{
	if(keystate[SDL_SCANCODE_LEFT])
		{
			posPlayer.x -= 2;
		}
	if(keystate[SDL_SCANCODE_RIGHT])
		{
			posPlayer.x += 2;
		}
	if(keystate[SDL_SCANCODE_UP])
		{
			posPlayer.y -= 2;
		}
	if(keystate[SDL_SCANCODE_DOWN])
		{
			posPlayer.y += 2;
		}
	if ((posPlayer.x + SPRITE_WIDTH) > SCREEN_WIDTH)
		{
			posPlayer.x = (SCREEN_WIDTH - SPRITE_WIDTH);
		}
	if ((posPlayer.y + SPRITE_HEIGHT) > SCREEN_HEIGHT)
		{
			posPlayer.y = (SCREEN_HEIGHT - SPRITE_HEIGHT);
		}

}

void thePlayer::show_player()
{
	SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer);
	SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));

}


theBullet::theBullet()
{
	/*posBullet.x;
	posBullet.y;
	posBullet.w = 10;
	posBullet.h = 15;*/
}

void theBullet::bullet_movement()
{
	/*if(keystate[SDL_SCANCODE_SPACE])
	{
		posBullet.x = posPlayer.x + 25;
		posBullet.y = posPlayer.y + 10;
	}

	posBullet.y -= 2;

	if(posBullet.y < 0)
	{
		posBullet.y = -50;

	}*/
}

void theBullet::show_bullet()
{
	//SDL_BlitSurface(Bullet, NULL, ScreenSurface, &posBullet);
	//SDL_SetColorKey(Bullet, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));//removes white background
}


theEnemy::theEnemy()
{
	srand (time(NULL));

	posEnemy.x = rand() % 300 + 50;
	posEnemy.y =0;
	posEnemy.w = 35;
	posEnemy.h = 60;

}

void theEnemy::enemy_movement()
{
	posEnemy.y += 1;

	if(posEnemy.y > SCREEN_HEIGHT)
	{
		posEnemy.y = SCREEN_HEIGHT +50;

	}
}

void theEnemy::show_enemy()
{
	SDL_BlitSurface(Enemy, NULL, ScreenSurface, &posEnemy);
	SDL_SetColorKey(Enemy, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));
}



bool initialise()
{
	bool success = true;

	if (SDL_Init(SDL_INIT_EVERYTHING) !=0)
	{
		cout<<"SDL_Init Error."<<SDL_GetError()<<endl;
		success = false;
	}
	else
	{
		//create the window for game
		Window = SDL_CreateWindow("Scrolling Shooter Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
		if (Window == NULL)
		{
			cout<<"Window Error"<<SDL_GetError()<<endl;
			success = false;
		}
		else
		{
			//get window surface
			ScreenSurface = SDL_GetWindowSurface(Window);
		}
	}
	return success;
}

bool LoadingMedia()
{
	bool success = true;

	Background = SDL_LoadBMP("background.bmp");
	if (Background == NULL)
	{
		cout<<"Error in loading background."<<SDL_GetError()<<endl;
		success = false;
	}
	
	Player = SDL_LoadBMP("spaceship.bmp");
	if (Player == NULL)
	{
		cout<<"Error in loading player."<<SDL_GetError()<<endl;
		success = false;
	}
	
	Enemy = SDL_LoadBMP("enemy.bmp");
	if (Enemy == NULL)
	{
		cout<<"Error in loading enemy."<<SDL_GetError()<<endl;
		success = false;
	}

	Bullet = SDL_LoadBMP("bullet.bmp");
	if (Bullet == NULL)
	{
		cout<<"Error in loading bullet."<<SDL_GetError()<<endl;
		success = false;
	}
	
	return success;
}
	
void closedown()
{
	SDL_FreeSurface(Background);
	Background = NULL;
	SDL_FreeSurface(Player);
	Player = NULL;
	SDL_FreeSurface(Enemy);
	Enemy = NULL;

	SDL_DestroyWindow(Window);
	Window = NULL;

	SDL_Quit();
}

int main(int argc, char** argv)
{
	bool quit = false;
	thePlayer myPlayer;
	theEnemy myEnemy;
	theBullet myBullet;

	if (!initialise())
	{
		cout<<"Failed to initialise"<<SDL_GetError()<<endl;
	}
	else
	{
		if (!LoadingMedia())
		{
			cout<<"Error loading media"<<SDL_GetError()<<endl;
		}
	}

	//makes all bullets false
	for (int i=0; i<MAX_BULLETS; i++)
	{
		arrayofBullets[i].isActive = false;
	}

	//GAME LOOP
	while (quit == false)
	{
		SDL_BlitSurface(Background, NULL, ScreenSurface, NULL);
		myPlayer.show_player();
		myPlayer.player_movement();

		while (SDL_PollEvent(&event))
		{
			if( event.type == SDL_QUIT )
            {
                quit = true;
				break;
            }
		
		if(keystate[SDL_SCANCODE_SPACE])
		{
			for (int i=0; i<MAX_BULLETS; i++)
			{
				if (arrayofBullets[i].isActive == false)
				{
					arrayofBullets[i].x_position = posPlayer.x + 25;
					arrayofBullets[i].y_position = posPlayer.y + 10;
					arrayofBullets[i].isActive = true;
					break;
				}
			}
		}
		//update game objects
		for (int i=0; i<MAX_BULLETS; i++)
		{
			if (arrayofBullets[i].isActive == true)
			{
				arrayofBullets[i].y_position -= 2;
			
				if (arrayofBullets[i].y_position < 0)
				{
					arrayofBullets[i].isActive = false;
				}
			}
		}

		for (int i=0; i<MAX_BULLETS; i++)
		{
			if (arrayofBullets[i].isActive == true)
			{
				SDL_BlitSurface(Bullet, NULL, ScreenSurface, NULL);
			}
		}
		
		}
		//myPlayer.show_player();
		//myBullet.show_bullet();
		//myEnemy.show_enemy();
		//myPlayer.player_movement();
		//myBullet.bullet_movement();
		//myEnemy.enemy_movement();
		
		SDL_UpdateWindowSurface(Window); //updates screen
	}

	closedown();

	return 0;

}


Output:
1
2
3
Line 16: error: SDL.h: No such file or directory
Line 16: error: expected constructor, destructor, or type conversion before '*' token
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: