//The headers #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" #include #include #include #include //Screen attributes const int SCREEN_WIDTH = 1000; const int SCREEN_HEIGHT = 580; const int SCREEN_BPP = 32; //The surfaces SDL_Surface *screen = NULL; SDL_Surface *water = NULL; SDL_Surface *grass = NULL; SDL_Surface *gubbe = NULL; SDL_Surface *square = NULL; //The event structure SDL_Event event; //The font TTF_Font *font = NULL; //The color of the font SDL_Color textColor = { 0, 0, 0 }; void seed() { //this is to get some better values out of the randomize funktion int stime; long ltime; /* get the current calendar time */ ltime = time(NULL); stime = (unsigned) ltime/2; srand(stime); } SDL_Surface *load_image( std::string filename ) { //The image that's loaded SDL_Surface* loadedImage = NULL; //The optimized surface that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = IMG_Load( filename.c_str() ); //If the image loaded if( loadedImage != NULL ) { //Create an optimized surface optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old surface SDL_FreeSurface( loadedImage ); //If the surface was optimized if( optimizedImage != NULL ) { //Color key surface SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } //Return the optimized surface return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) { //Holds offsets SDL_Rect offset; //Get offsets offset.x = x; offset.y = y; //Blit SDL_BlitSurface( source, clip, destination, &offset ); } bool init() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } //Set up the screen screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); //If there was an error in setting up the screen if( screen == NULL ) { return false; } //Initialize SDL_ttf if( TTF_Init() == -1 ) { return false; } //Set the window caption SDL_WM_SetCaption( "Press an Arrow Key", NULL ); //If everything initialized fine return true; } bool load_files() { //Load the grass image grass = load_image( "grass.png" ); //water water = load_image("water.png"); //gubbe gubbe = load_image("gubbe.png"); //square square = load_image("square.png"); //Open the font font = TTF_OpenFont( "lazy.ttf", 72 ); //If there was a problem in loading the background if( grass == NULL ) { printf("Error loading grass..."); return false; } printf("Loaded the grass..."); //If there was an error in loading the font if( font == NULL ) { return false; } //If everything loaded fine return true; } void clean_up() { //Free the surfaces SDL_FreeSurface( grass ); SDL_FreeSurface( water ); SDL_FreeSurface( gubbe ); SDL_FreeSurface( square ); //Close the font TTF_CloseFont( font ); //Quit SDL_ttf TTF_Quit(); //Quit SDL SDL_Quit(); } class Background { private: int background[58][100]; public: Background(); int getBackgroundType(int x, int y); void paintBackground(); void putBackground(int x, int y); }; Background::Background() { printf("\nGenerating map"); int row, col; for(row = 0; row<58; row++) { for(col=0; col<100; col++) { background[row][col] = 0; if(rand() %20 >= 19) background[row][col] = rand() % 2; } } } void Background::paintBackground() { int row,col; for(row=0; row<58; row++) { for(col=0; col<100; col++) { if(background[row][col] == 1) apply_surface(10*col,10*row,water,screen); else apply_surface(10*col,10*row,grass,screen); } } } int Background::getBackgroundType(int col, int row) { return background[row][col]; } void Background::putBackground(int col, int row) { if(background[row][col] == 1) { printf("\nPrinting water at %d, %d\n",col*10,row*10); apply_surface(col*10,row*10,water,screen); } else { printf("\nPrinting grass at %d, %d\n",col*10,row*10); apply_surface(col*10,row*10,grass,screen); } } //Doing a global background for us to be able to easy change the values in it. Background bground; //UNIT CLASS class Unit { private: int _x; int _y; int _health; int _moves; int _attack; int _defense; int _special; int _type; public: Unit(); int move(); int getX(); int getY(); int getAttack(); int getDefense(); int getSpecial(); int initUnit(int,int,int,int,int,int); void moveRight(); void moveLeft(); void moveUp(); void moveDown(); }; Unit::Unit() { } int Unit::initUnit(int health, int speed, int attack, int defense, int special,int type) { _health = health; _moves = speed; _attack = attack; _defense = defense; _special = special; _type = type; _x = (rand() % 100); _y = (rand() % 50); return 0; } int Unit::getX() { return _x; } int Unit::getY() { return _y; } void Unit::moveRight() { if(_moves){ printf("\n Trying to move right, putting background at %d,%d\n",_x,_y); bground.putBackground(_x,_y); bground.putBackground(_x,_y+1); _x++; _moves--; } } void Unit::moveLeft() { if(_moves){ printf("\n Trying to move left, putting background at %d,%d\n",_x,_y); bground.putBackground(_x,_y); bground.putBackground(_x,_y+1); _x--; _moves--; } } void Unit::moveUp() { if(_moves){ printf("\n Trying to move up, putting background at %d,%d\n",_x,_y); bground.putBackground(_x,_y); bground.putBackground(_x,_y+1); _y--; _moves--; } } void Unit::moveDown() { if(_moves){ printf("\n Trying to move down, putting background at %d,%d\n",_x,_y); bground.putBackground(_x,_y+1); bground.putBackground(_x,_y); _y++; _moves--; } } class UnitManager { private: int quantity; Unit Unitlist[100]; int selected; public: int newUnit(); UnitManager(); int display(); int getX(int); int getY(int); int select(int); void displaySelect(); void moveRight(); void moveLeft(); void moveUp(); void moveDown(); }; int UnitManager::newUnit() { if(quantity <100) { Unitlist[quantity].initUnit(100,5,100,100,100,1); quantity++; } return 0; } int UnitManager::getX(int unit) { return Unitlist[unit].getX(); } int UnitManager::getY(int unit) { return Unitlist[unit].getY(); } UnitManager::UnitManager() { quantity = 0; selected = 0; } int UnitManager::display() { int i; for(i = 0; i <= quantity; i++) { apply_surface(Unitlist[i].getX()*10,Unitlist[i].getY()*10,gubbe,screen); } return 0; } int UnitManager::select(int forward) { bground.putBackground(Unitlist[selected].getX(),Unitlist[selected].getY()+1); if(forward) selected++; if(selected >= quantity) selected = 0; printf("\nSelected unit: %d of total %d units\n",selected,quantity); } void UnitManager::displaySelect() { bground.putBackground(Unitlist[selected].getX(),Unitlist[selected].getY()+1); apply_surface(Unitlist[selected].getX()*10,Unitlist[selected].getY()*10+10,square,screen); } void UnitManager::moveRight() { if(bground.getBackgroundType(Unitlist[selected].getX()+1,Unitlist[selected].getY()+1) != 1) Unitlist[selected].moveRight(); } void UnitManager::moveLeft() { if(bground.getBackgroundType(Unitlist[selected].getX()-1,Unitlist[selected].getY()+1) != 1) Unitlist[selected].moveLeft(); } void UnitManager::moveUp() { if(bground.getBackgroundType(Unitlist[selected].getX(),Unitlist[selected].getY()) != 1) Unitlist[selected].moveUp(); } void UnitManager::moveDown() { if(bground.getBackgroundType(Unitlist[selected].getX(),Unitlist[selected].getY()+2) != 1) Unitlist[selected].moveDown(); } int main( int argc, char* args[] ) { //Quit flag bool quit = false; seed(); printf("\niniting...."); //Initialize if( init() == false ) { return 1; } printf("\n Init OK! \n loading the files!"); //Load the files if( load_files() == false ) { return 1; } printf("\nThe files loaded ok!"); //Player ones units UnitManager p1Units; p1Units.newUnit(); p1Units.newUnit(); printf("\nPainting the map\n"); bground.paintBackground(); //While the user hasn't quit while( quit == false ) { p1Units.display(); //While there's events to handle if( SDL_PollEvent( &event ) ) { if(event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym ){ case SDLK_TAB: { p1Units.select(1); p1Units.displaySelect(); break; } case SDLK_LEFT: { p1Units.moveLeft(); p1Units.displaySelect(); break; } case SDLK_RIGHT: { p1Units.moveRight(); p1Units.displaySelect(); break; } case SDLK_UP: { p1Units.moveUp(); p1Units.displaySelect(); break; } case SDLK_DOWN: { p1Units.moveDown(); p1Units.displaySelect(); break; } case SDLK_n: { p1Units.newUnit(); break; } } } //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } } //Clean up clean_up(); return 0; }