/* SDL requires the following linker entries: mingw32 (if you're using GCC or G++) sdl sdlmain Don't forget to add your SDL directory to the compiler's include search paths. */ #include #include using namespace std; #include "SDL.h" // My framerate controls #define DESIREDFPS 20 #define WAITTIME int(1000.0 / DESIREDFPS) SDL_Surface* screen; // Mouse coords int mx = 0, my = 0; // My own mouse handling functions #ifndef _MouseAt #define _MouseAt inline bool mouseAt(int x1, int y1, int x2, int y2) { // Designed to execute quickly. Doesn't check for overlapping boundaries. return (mx >= x1) && (mx <= x2) && (my >= y1) && (my <= y2); } inline bool mouseAt(SDL_Rect rect) { // Designed to execute quickly. Doesn't check for overlapping boundaries. return (mx >= rect.x) && (mx <= rect.x + rect.w) && (my >= rect.y) && (my <= rect.y + rect.h); } #endif bool initSDL(int w, int h, bool fullscreen) { if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { printf("Couldn't initialize SDL: %s\n", SDL_GetError()); return 0; } atexit(SDL_Quit); screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE | (fullscreen? SDL_FULLSCREEN : 0)); if ( screen == NULL ) { printf("Couldn't set video mode %dx%d: %s\n", w, h, SDL_GetError()); return 0; } //printf(" running @ %dx%d %dbpp (done)\n", w,h,screen->format->BitsPerPixel); return 1; } //end bool initSDL(int w, int h, bool fullscreen) void drawIMG(SDL_Surface* img, int x, int y) { SDL_Rect r; r.x = x; r.y = y; SDL_BlitSurface(img, NULL, screen, &r); } void startGame() { // Load a background pic //SDL_Surface* back = SDL_LoadBMP("bg.bmp"); // Set up main loop SDL_Event event; int frameStartTime; int frameTime; float fps; bool done = 0; while (!done) { frameStartTime = SDL_GetTicks(); // Get mouse coords SDL_GetMouseState(&mx, &my); // Handle all events while (SDL_PollEvent(&event)) { // Handle a closing window if(event.type == SDL_QUIT) { exit(0); } //quit if(event.type == SDL_KEYDOWN) { // Do something with single keypresses if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; } } if(event.type == SDL_MOUSEBUTTONDOWN) { // Do something with the mouse //if(mouseAt(0,0, 50, 50)) // return; } } // Draw background pic to the screen surface //SDL_BlitSurface(back, NULL, screen, NULL); // or just clear the screen SDL_FillRect(screen, NULL, 0x000000); // Send everything to the monitor (waits for VSYNC) SDL_Flip(screen); // Frame control frameTime = SDL_GetTicks()-frameStartTime; if (frameTime < WAITTIME) SDL_Delay(WAITTIME - frameTime); frameTime = SDL_GetTicks()-frameStartTime; if (frameTime != 0) fps = 1000.0f / frameTime; //this is the framerate with vsync and the framebreak (should be equal to 1000/WAITTIME) else fps = 1111.11f; // End Frame Control } return; } // SDL requires main to be declared this way int main(int argc, char *argv[]) { SDL_putenv("SDL_VIDEO_CENTERED=center"); initSDL(800, 600, 0); SDL_WM_SetCaption("Sample SDL", NULL); //SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL); // Seed the random number generator srand(time(NULL)); // Now do whatever we want! startGame(); return 0; }