Skip to content

Instantly share code, notes, and snippets.

@cuu
Created April 5, 2019 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuu/a2cae0e517a0af49b92ca9fc72472045 to your computer and use it in GitHub Desktop.
Save cuu/a2cae0e517a0af49b92ca9fc72472045 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
int main() {
int windowHeight = 240;
int windowWidth = 320;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Initialization failed\n");
return 1;
}
SDL_Window *window = SDL_CreateWindow("Practice making sdl Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth,
windowHeight, SDL_WINDOW_SHOWN);
if (window == NULL) {
SDL_Quit();
return 2;
}
// We create a renderer with hardware acceleration, we also present according with the vertical sync refresh.
SDL_Renderer *s = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) ;
int quit = 0;
SDL_Event event;
int x=0;
int y=0;
int color = 0;
int frames = 0;
int fps = 0;
int prev_time = SDL_GetTicks();
int current_time = SDL_GetTicks();
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = 1;
}
if(event.type == SDL_KEYDOWN) {
if ( event.key.keysym.sym == SDLK_q || event.key.keysym.sym == SDLK_ESCAPE) {
printf("Quiting...\n");
quit=1;
break;
}
}
}
SDL_RenderClear(s);
for (x=0;x<windowWidth;x++) {
for(y=0;y<windowHeight;y++) {
color = rand()%0xff;
SDL_SetRenderDrawColor(s, color,color,color, 0xFF);
SDL_RenderDrawPoint(s,x,y);
}
}
SDL_SetRenderDrawColor(s, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderPresent(s);
frames++;
current_time = SDL_GetTicks();
if( (current_time - prev_time) > 10000) {
fps = frames/10;
printf("fps is %d\n",fps);
frames = 0;
prev_time = current_time;
}
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(s);
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment