#include "engine.h" Engine::Engine()//Create the class and assign default values { SDL_Init( SDL_INIT_VIDEO ); //We start SDL, just video at the moment screen=NULL; tileset=NULL; SDL_WM_SetCaption( "Life Game - Tile test", NULL ); //Caption of the window SDL_ShowCursor(SDL_DISABLE); // hide system mouse TileSize =50; // size of the default tile 50 pixels x 50 pixels ScreenX=800; //screen width ScreenY=600; //screen height bpp=32; //screen bpp StartTick = SDL_GetTicks();//Initialize timer Frames=0; //Initialize frames = 0 } Engine::~Engine()//Destructor { //Free all the surfaces if (screen!=NULL)SDL_FreeSurface(screen); //Free screen surface if (tileset!=NULL)SDL_FreeSurface(tileset); //Free tileset surface SDL_Quit(); //Close SDL } void Engine::SetVga() // set video mode { screen = SDL_SetVideoMode(ScreenX, ScreenY, bpp,SDL_SWSURFACE|SDL_DOUBLEBUF); /* It will set up a windowed video mode (width=screenx, height = screeny, bpp = bpp) if we want to use a fullscreen mode we have to add "|SDL_FULLSCREEN" after SDL_DOUBLEBUF */ } void Engine::FillColour(Uint8 r, Uint8 g, Uint8 b)//Fill the screen with a colour { SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, r, g, b));// r-ed, g-reen,b-lue } void Engine::Flip()//flips backbuffer into primary buffer(screen) { SDL_Flip(screen); } void Engine::LoadTileset() //loads an image into a memory (tileset), assigns the colorkey { SDL_Surface* TmpSurface = NULL; //Temporary surface for loading TmpSurface = IMG_Load( "tileset.png"); //Load the image into a temporary surface if( TmpSurface != NULL ) { tileset= SDL_DisplayFormat( TmpSurface );//optimize surface SDL_FreeSurface( TmpSurface); //free temporary surface } SDL_SetColorKey(tileset,SDL_SRCCOLORKEY | SDL_RLEACCEL,SDL_MapRGB(screen -> format, 255, 0, 255)); //set the color key } void Engine::UpdateMouse() { MouseButtons = SDL_GetMouseState(&MouseX, &MouseY); //Update mouse state SDL_Rect start,end; //set the cliping for the source surface (the tileset) start.x= 200; start.y=0; start.w=TileSize; start.h=TileSize; //set the cliping for the destination surface (the screen) end.x = MouseX; end.y = MouseY; end.w = TileSize; end.h = TileSize; SDL_BlitSurface(tileset, &start, screen, &end); //Blits the mouse pointer into the screen } void Engine::BlitTileset(int x, int y, int tile) //Blits a tile from our tileset to the screen { SDL_Rect start,end; //set the cliping for the source surface (the tileset) start.x= tile*TileSize; start.y=0; start.w=TileSize; start.h=TileSize; //set the cliping for the destination surface (the screen) end.x = (x-1)*TileSize; end.y = (y-1)*TileSize; end.w = TileSize; end.h = TileSize; SDL_BlitSurface(tileset, &start, screen, &end); //Blits a tile from our tileset to the screen } void Engine::Wait(Uint32 ms) //Just pause for ms miliseconds { SDL_Delay(ms); } int Engine::TimeElapsed() //Count time elapsed { return SDL_GetTicks() - StartTick; } void Engine::UpdateTimer() //Reset time counter { StartTick = SDL_GetTicks(); }
Wednesday, December 29, 2010
Code::Blocks(4) C++ SDL and Life "engine.cpp"
Here is the code for the engine class implementation, just copy and paste into file engine.cpp in the project, then save the project:
Labels:
Code::Blocks,
Life Game
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment