// Author : Jovian HERSEMEULE #include #include int main() { /// [1] Start // [1.1] Start SDL if ( SDL_Init( SDL_INIT_VIDEO ) < 0) { std::cout << "Can't initialize SDL: " << SDL_GetError() << std::endl; return 1; } // [1.2] Anticipate quit atexit(SDL_Quit); // [1.3] Set title SDL_WM_SetCaption("Application SDL", 0); /// [2] Create components // [2.1] Create window const int width(640), height(480); SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); if ( !screen ) { std::cout << "Unable to set " << width << "x" << height << " video: " << SDL_GetError() << std::endl; return 1; } // [2.2] Other components SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp"); SDL_Surface* pawn = SDL_LoadBMP("textures/pawn.bmp"); SDL_Rect pos({0, 0, 0, 0}); SDL_Rect pawnPos({0, 0, 0, 0}); /// [3] Main loop bool done(false); while (!done) { // [3.1] Events SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: done = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) done = true; break; case SDL_MOUSEMOTION: pawnPos.x = event.motion.x - pawn->w / 2; pawnPos.y = event.motion.y - pawn->h / 2; break; } // end switch event type } // end of message processing // [3.2] Compute // Nothing for now // [3.3] Draw phase SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255)); for (pos.y = 0; pos.y < height; pos.y += grass->h) for (pos.x = 0; pos.x < width; pos.x += grass->w) SDL_BlitSurface(grass, NULL, screen, &pos); SDL_BlitSurface(pawn, NULL, screen, &pawnPos); SDL_Flip(screen); SDL_Delay(16); } // end of main loop ///[4] Free components SDL_FreeSurface(grass); SDL_FreeSurface(pawn); SDL_FreeSurface(screen); std::cout << "No error caught." << std::endl; return 0; }