Last active
November 16, 2020 04:03
-
-
Save Costava/6d5a82cd435d43387eadef43cf531317 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. | |
// Every frame: | |
// - Set every pixel in array to fully opaque rand color. | |
// - SDL_UpdateTexture | |
// - SDL_RenderCopy | |
// - SDL_RenderPresent | |
// | |
// Tested with SDL2 version 2.0.12 | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <time.h> | |
#include <SDL2/SDL.h> | |
// Equivalent to malloc but if error then print to stderr and exit. | |
void *checked_malloc(size_t size) { | |
void *const ptr = malloc(size); | |
if (size != 0 && ptr == NULL) { | |
fprintf(stderr, "%s: Failed to malloc %zu bytes\n", __func__, size); | |
exit(1); | |
} | |
return ptr; | |
} | |
// SDL_Init but if error then print to stderr and 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); | |
} | |
} | |
// SDL_CreateWindow but if error then print to stderr and 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; | |
} | |
// SDL_CreateRenderer but if error then print to stderr and exit. | |
SDL_Renderer *sdlu_create_renderer( | |
SDL_Window *const window, | |
const int index, | |
const uint32_t flags) | |
{ | |
SDL_Renderer *const ren = SDL_CreateRenderer(window, index, flags); | |
if (ren == NULL) { | |
fprintf(stderr, "%s: Failed to create renderer using index: %d " | |
"and flags: %d. Error: %s\n", __func__, index, | |
flags, SDL_GetError()); | |
exit(1); | |
} | |
return ren; | |
} | |
// SDL_CreateTexture but if error then print to stderr and exit. | |
SDL_Texture *sdlu_create_texture( | |
SDL_Renderer *const renderer, | |
const uint32_t format, | |
const int access, | |
const int w, | |
const int h) | |
{ | |
SDL_Texture *const tex = SDL_CreateTexture(renderer, format, access, w, h); | |
if (tex == NULL) { | |
fprintf(stderr, "%s: Failed to create texture: %s\n", | |
__func__, SDL_GetError()); | |
exit(1); | |
} | |
return tex; | |
} | |
// SDL_UpdateTexture but if error then print to stderr and exit. | |
void sdlu_update_texture( | |
SDL_Texture *const texture, | |
const SDL_Rect *const rect, | |
const void *const pixels, | |
int pitch) | |
{ | |
const int code = SDL_UpdateTexture(texture, rect, pixels, pitch); | |
if (code != 0) { | |
fprintf(stderr, "%s: Failed to update texture: %s\n", | |
__func__, SDL_GetError()); | |
exit(1); | |
} | |
} | |
// SDL_RenderCopy but if error then print to stderr and exit. | |
void sdlu_render_copy( | |
SDL_Renderer *const renderer, | |
SDL_Texture *const texture, | |
const SDL_Rect *const srcrect, | |
const SDL_Rect *const dstrect) | |
{ | |
const int code = SDL_RenderCopy(renderer, texture, srcrect, dstrect); | |
if (code != 0) { | |
fprintf(stderr, "%s: Failed to RenderCopy: %s", | |
__func__, SDL_GetError()); | |
exit(1); | |
} | |
} | |
int main(void) { | |
sdlu_init(SDL_INIT_VIDEO); | |
const int window_w = 800; | |
const int window_h = 600; | |
SDL_Window *const window = sdlu_create_window( | |
"Demo", | |
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
window_w, window_h, 0); | |
SDL_Renderer *const renderer = sdlu_create_renderer(window, -1, SDL_RENDERER_ACCELERATED); | |
SDL_Texture *const texture = sdlu_create_texture( | |
renderer, | |
SDL_PIXELFORMAT_ABGR8888, | |
SDL_TEXTUREACCESS_TARGET, | |
window_w, window_h); | |
const int pixels_len = window_w * window_h; | |
const int pixels_pitch = window_w * sizeof(uint32_t); | |
uint32_t *const pixels = checked_malloc(sizeof(uint32_t) * pixels_len); | |
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 i = 0; i < pixels_len; i += 1) { | |
// Fully opaque. | |
pixels[i] = rand() | 0xff000000; | |
} | |
sdlu_update_texture(texture, NULL, pixels, pixels_pitch); | |
sdlu_render_copy(renderer, texture, NULL, NULL); | |
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 -g -O3 -std=c99 | |
EXECUTABLE:=rand_rgb_window_SDL_UpdateTexture | |
################################################################################ | |
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