#include #include #include #include #include const int WINDOW_WIDTH = 1024; const int WINDOW_HEIGHT = 768; const float scale_x = (float)WINDOW_WIDTH/(float)0xfff; const float scale_y = (float)WINDOW_HEIGHT/(float)0x7ff; struct square { Uint16 x; Uint16 y; Uint16 w; Uint16 h; Uint8 r; Uint8 g; Uint8 b; Uint8 a; }; struct squares { size_t n; struct square * square; }; void square_build(struct squares * squares) { int color, pos; for(size_t i = 0; i < squares->n; i++) { SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, "Build square %8d\r", i); color = rand(); squares->square[i].r = (color) & 0xff; squares->square[i].g = (color >> 8) & 0xff; squares->square[i].b = (color >> 16) & 0xff; squares->square[i].a = (color >> 24) & 0xff; pos = rand(); squares->square[i].x = (((pos) & 0x7ff) << 1) * scale_x; // 11 bits * 2 squares->square[i].y = (((pos >> 11) & 0x3ff) << 1) * scale_y; // 10 bits * 2 squares->square[i].w = ((pos >> 21) & 0x1f); // 5 bits squares->square[i].h = ((pos >> 27) & 0x1f); // 5 bits } } struct squares * squares_init() { SDL_Log("Init squares\r"); struct squares * squares = malloc(sizeof *squares); squares->n = 1; squares->square = malloc(squares->n * sizeof(struct square)); square_build(squares); return squares; } void squares_rebuild(struct squares * squares) { SDL_Log("Rebuild squares %8d\r", squares->n); squares->square = realloc(squares->square, squares->n * sizeof(struct square)); square_build(squares); } void squares_destroy(struct squares * squares) { if(squares) if(squares->square) free(squares->square); free(squares); } int main( int argc, char **argv ) { SDL_Init( SDL_INIT_EVERYTHING ); atexit(SDL_Quit); SDL_Window *window = SDL_CreateWindow( "Frames", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN ); SDL_Renderer *renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED); SDL_RenderSetLogicalSize(renderer, WINDOW_WIDTH, WINDOW_HEIGHT); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); int frameCount = 0; Uint32 time = SDL_GetTicks(); FPSmanager fpsManager; SDL_initFramerate( &fpsManager ); SDL_setFramerate( &fpsManager, 60); SDL_Rect rect; struct squares *squares = squares_init(); bool bRunning = true; while (bRunning) { SDL_Event ev; while ( SDL_PollEvent( & ev )) // Check event queue. { switch( ev.type ) { case SDL_QUIT: // when window is closed bRunning = false; break; case SDL_KEYDOWN: switch ( ev.key.keysym.sym ) { case SDLK_1: squares->n = 1; squares_rebuild(squares); break; case SDLK_UP: squares->n *= 2; squares_rebuild(squares); break; case SDLK_DOWN: squares->n /= 2; squares_rebuild(squares); break; case SDLK_q: bRunning = false; break; } break; } } SDL_SetRenderDrawColor (renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear (renderer); struct square *tsq = squares->square; for(size_t i = 0; i < squares->n; i++) { SDL_SetRenderDrawColor (renderer, tsq->r, tsq->g, tsq->b, tsq->a); rect = (SDL_Rect) { tsq->x, tsq->y, tsq->w, tsq->h }; SDL_RenderFillRect(renderer, &rect); tsq++; } // Determine when one second has passed if ( SDL_GetTicks() - time > 1000 ) { fprintf(stdout, "FPS %8d - Rects %8d\r", frameCount, squares->n); fflush(stdout); frameCount = 0; time = SDL_GetTicks(); } else { ++frameCount; } SDL_RenderPresent(renderer); SDL_framerateDelay(&fpsManager); } SDL_DestroyRenderer(renderer); renderer = NULL; SDL_DestroyWindow(window); window = NULL; return 0; }