Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created February 20, 2015 00:49
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 grondilu/588b78f4838bad93ba53 to your computer and use it in GitHub Desktop.
Save grondilu/588b78f4838bad93ba53 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <SDL2/SDL.h>
unsigned int frames = 0;
unsigned int t_acc = 0;
SDL_Window *window;
SDL_Surface *surface;
SDL_Renderer *renderer;
SDL_Point points[320 * 240];
void print_fps ()
{
static Uint32 last_t = 0;
Uint32 t = SDL_GetTicks();
Uint32 dt = t - last_t;
t_acc += dt;
if (t_acc > 1000)
{
unsigned int el_time = t_acc / 1000;
printf("- fps: %g\n",
(float) frames / (float) el_time);
t_acc = 0;
frames = 0;
}
last_t = t;
}
void blit_noise()
{
unsigned int i, j, count;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
count = 0;
for (i=0; i < surface->w; ++i) {
for (j=0; j < surface->h; ++j) {
if (rand() & 1) {
SDL_Point p = { i, j };
points[count++] = p;
}
}
}
SDL_RenderDrawPoints(renderer, points, count);
SDL_RenderPresent(renderer);
++frames;
print_fps();
}
int main(int argc, char *argv[]) {
int i;
printf("initializing SDL... %d\n",
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO)
);
window = SDL_CreateWindow(
"my window",
0, 0,
320, 240,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer(window, -1, 0);
surface = SDL_GetWindowSurface(window);
int pixelformat = SDL_GetWindowPixelFormat(window);
printf("pixel format is %x\n", pixelformat);
printf("pixel type is %x\n", SDL_PIXELTYPE(pixelformat));
printf("pixel order is %x\n", SDL_PIXELORDER(pixelformat));
printf("pixel layout is %x\n", SDL_PIXELLAYOUT(pixelformat));
printf("%d bits per pixel\n", SDL_BITSPERPIXEL(pixelformat));
printf("%d bytes per pixel\n", SDL_BYTESPERPIXEL(pixelformat));
printf("%s a palette\n", SDL_ISPIXELFORMAT_INDEXED(pixelformat) ? "has" : "does not have");
for (i=0;i<100;i++) {
blit_noise();
}
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment