Created
September 28, 2020 23:20
-
-
Save Costava/51657553cc72cbbdfedc9fbf09f4fd05 to your computer and use it in GitHub Desktop.
SDL_Texture color component order in pixels array (SDL2)
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
// If a texture has format SDL_PIXELFORMAT_ABGR8888, | |
// then the red component is the first byte in the pixels array of the texture | |
// Tested on a little-endian (x86) system | |
// Would the red component also come first in the array on a big-endian system? | |
// | |
// Tested with SDL2 version 2.0.12 | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
// For the commented out FPS code | |
// #include <time.h> | |
#include <SDL2/SDL.h> | |
// Equivalent to malloc but if error then print to stderr and exit | |
void *easy_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; | |
} | |
// // Not needed | |
// SDL_Renderer* sdlu_get_renderer(SDL_Window *const window) { | |
// SDL_Renderer *const ren = SDL_GetRenderer(window); | |
// | |
// if (ren == NULL) { | |
// fprintf(stderr, "%s: Failed to get renderer: %s\n", | |
// __func__, SDL_GetError()); | |
// exit(1); | |
// } | |
// | |
// return ren; | |
// } | |
// 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( | |
"sdl2_tex_component_order", | |
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 = | |
easy_malloc(sizeof(uint32_t) * pixels_len); | |
for (int i = 0; i < pixels_len; i += 1) { | |
// pixels[i] = 0xff0000ff;// Full-opaque red | |
pixels[i] = 0xff00ffff;// Full-opaque yellow | |
// pixels[i] = 0xffff0000;// Full-opaque blue | |
} | |
sdlu_update_texture(texture, NULL, pixels, pixels_pitch); | |
sdlu_render_copy(renderer, texture, NULL, NULL); | |
SDL_RenderPresent(renderer); | |
// Frames per second printing is commented out because it is not helpful | |
// since the screen is updated only once | |
// 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); | |
// | |
// 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 -O0 -std=c99 | |
EXECUTABLE:=sdl2_tex_component_order | |
################################################################################ | |
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