#include #include #include void init(); // initialize SDL video // global window and renderer variable SDL_Renderer *canvas; SDL_Window *window; class Shape { private: int x,y; unsigned color; public: Shape(int x, int y, unsigned color):x(x),y(y),color(color){} unsigned getColor()const{return color;} int getx()const{return x;} int gety()const{return y;} }; class Rect : public Shape { private: int w,h; public: Rect(int x, int y, unsigned color, int w, int h):Shape(x,y,color),w(w),h(h){} void draw() const{rectangleColor(canvas,getx(),gety(),getx()+w,gety()+h,getColor());} }; class Circle : public Shape { private: int r; public: Circle(int x, int y, unsigned color, int r):Shape(x,y,color),r(r){} void draw() const{circleColor(canvas,getx(),gety(),r,getColor());} }; int main ( int argc, char** argv ) { init(); SDL_RenderClear(canvas); SDL_RenderPresent(canvas); Rect r(100,200,0xff0000ff,20,30); Circle c(200,100,0xff00ffff,80); r.draw(); c.draw(); // r.h=2; // KO // r.x=4; // KO // finally, update the renderer :) SDL_RenderPresent(canvas); bool done=false; while(!done) { // check for messages SDL_Event event; SDL_WaitEvent(&event); switch (event.type) { // exit if the window is closed case SDL_QUIT: done = true; break; // check for keypresses case SDL_KEYDOWN: // exit if ESCAPE is pressed if (event.key.keysym.sym == SDLK_ESCAPE) done = true; break; } // end switch } // end of message processing return 0; } void init() { // initialize SDL video if (SDL_Init(SDL_INIT_VIDEO) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); exit(3); } // make sure SDL cleans up before exit atexit(SDL_Quit); // create a new window if (SDL_CreateWindowAndRenderer(360, 360, SDL_WINDOW_SHOWN, &window, &canvas)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError()); exit(3); } SDL_DestroyRenderer(canvas); canvas = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); }