Created
November 16, 2020 01:40
-
-
Save Costava/269883684c2d5226fc62d77f4af88053 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sort of a benchmark --- if touching every pixel every frame, | |
// a real program should not hope to get a higher frames per second | |
// than this simple demo. | |
// | |
// Single process and thread. | |
// Using SDL_Renderer: | |
// - SDL_SetRenderDrawColor then SDL_RenderDrawPoint for each pixel each frame. | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <time.h> | |
#include <SDL2/SDL.h> | |
// Call SDL_Init with the given flags. | |
// If error, print to stderr and call exit. | |
void sdlu_init(const uint32_t flags) { | |
const int code = SDL_Init(flags); | |
if (code != 0) { | |
fprintf(stderr, "%s: SDL_Init returned %d instead of 0 for success. " | |
"[flags: %d] [Error: %s]\n", | |
__func__, code, | |
flags, SDL_GetError()); | |
exit(1); | |
} | |
} | |
// Call SDL_CreateWindow. | |
// If error, print to stderr and call exit. | |
SDL_Window *sdlu_create_window( | |
const char *const title, | |
const int x, | |
const int y, | |
const int w, | |
const int h, | |
const uint32_t flags) | |
{ | |
SDL_Window *const window = SDL_CreateWindow(title, x, y, w, h, flags); | |
if (window == NULL) { | |
fprintf(stderr, "%s: SDL_CreateWindow returned NULL. " | |
"[title: %s] [x: %d] [y: %d] [w: %d] [h: %d] " | |
"[flags: %d] [Error: %s]\n", | |
__func__, | |
title, x, y, w, h, | |
flags, SDL_GetError()); | |
exit(1); | |
} | |
return window; | |
} | |
// Call SDL_SetRenderDrawColor. | |
// If error, print to stderr and call exit. | |
void sdlu_set_render_draw_color( | |
SDL_Renderer *renderer, | |
uint8_t r, | |
uint8_t g, | |
uint8_t b, | |
uint8_t a) | |
{ | |
const int code = SDL_SetRenderDrawColor(renderer, r, g, b, a); | |
if (code != 0) { | |
fprintf(stderr, "%s: SDL_SetRenderDrawColor error: %s\n", __func__, SDL_GetError()); | |
exit(1); | |
} | |
} | |
// Call SDL_RenderDrawPoint. | |
// If error, print to stderr and call exit. | |
void sdlu_render_draw_point(SDL_Renderer *renderer, int x, int y) { | |
const int code = SDL_RenderDrawPoint(renderer, x, y); | |
if (code != 0) { | |
fprintf(stderr, "%s: SDL_RenderDrawPoint error: %s\n", __func__, SDL_GetError()); | |
exit(1); | |
} | |
} | |
int main(void) { | |
if (sizeof(int) < 3) { | |
fprintf(stderr, "Warning: sizeof(int) (%ld) is < 3 bytes. " | |
"Color will not be as expected.", sizeof(int)); | |
} | |
sdlu_init(SDL_INIT_VIDEO); | |
const int window_width = 800; | |
const int window_height = 600; | |
SDL_Window *const window = sdlu_create_window( | |
"Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width, window_height, 0); | |
SDL_Renderer *const renderer = SDL_CreateRenderer(window, -1, | |
SDL_RENDERER_ACCELERATED | |
// SDL_RENDERER_SOFTWARE | |
); | |
if (renderer == NULL) { | |
fprintf(stderr, "SDL_CreateRenderer error: %s\n", SDL_GetError()); | |
exit(1); | |
} | |
srand(time(NULL)); | |
uint32_t old_time = SDL_GetTicks(); | |
bool quit = false; | |
while (!quit) { | |
const uint32_t new_time = SDL_GetTicks(); | |
const uint32_t dt = new_time - old_time; | |
SDL_Event event; | |
while (SDL_PollEvent(&event) != 0) { | |
switch (event.type) { | |
case SDL_QUIT: | |
{ | |
quit = true; | |
break; | |
} | |
} | |
} | |
// Show FPS as 0 if dt is 0 (avoid division by zero). | |
const double fps = (dt == 0) ? 0 : 1000.0 / dt; | |
printf("dt: %d fps: %lf\n", dt, fps); | |
for (int y = 0; y < window_height; y += 1) { | |
for (int x = 0; x < window_width; x += 1) { | |
const int rand_int = rand(); | |
// Here we assume that sizeof(int) is >= 24 bits. | |
sdlu_set_render_draw_color(renderer, | |
rand_int >> 0, | |
rand_int >> 8, | |
rand_int >> 16, | |
SDL_ALPHA_OPAQUE); | |
sdlu_render_draw_point(renderer, x, y); | |
} | |
} | |
SDL_RenderPresent(renderer); | |
old_time = new_time; | |
} | |
SDL_Quit(); | |
return 0; | |
} | |
/* zlib License | |
Copyright (c) 2020 Costava | |
This software is provided 'as-is', without any express or implied | |
warranty. In no event will the authors be held liable for any damages | |
arising from the use of this software. | |
Permission is granted to anyone to use this software for any purpose, | |
including commercial applications, and to alter it and redistribute it | |
freely, subject to the following restrictions: | |
1. The origin of this software must not be misrepresented; you must not | |
claim that you wrote the original software. If you use this software | |
in a product, an acknowledgment in the product documentation would be | |
appreciated but is not required. | |
2. Altered source versions must be plainly marked as such, and must not be | |
misrepresented as being the original software. | |
3. This notice may not be removed or altered from any source distribution. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# makefile | |
CC:=gcc | |
CFLAGS:=-Wall -O3 -std=c99 | |
EXECUTABLE:=rand_rgb_window_SDL_Renderer | |
################################################################################ | |
run: build | |
./$(EXECUTABLE) | |
build: $(EXECUTABLE) | |
clean: | |
rm -f $(EXECUTABLE) | |
$(EXECUTABLE): ./$(EXECUTABLE).c | |
$(CC) --output $@ $(CFLAGS) -lSDL2 $^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment