Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dev001hajipro/4b8138d24732c5fb371ccdf7da46d4b6 to your computer and use it in GitHub Desktop.
Save dev001hajipro/4b8138d24732c5fb371ccdf7da46d4b6 to your computer and use it in GitHub Desktop.
pixel manipulation: SDL_LockTexture and SDL_MapRGBA sample on Emscripten.
pixel manipulation: SDL_LockTexture and SDL_MapRGBA sample on Emscripten.
# https://gamedev.stackexchange.com/questions/98641/how-do-i-modify-textures-in-sdl-with-direct-pixel-access
bellow code dont run.
SDL_PixelFormat pixelFormat;
pixelFormat.format = format;
# rewrite my code.
ctx->pixelFormat = SDL_AllocFormat(SDL_PIXELFORMAT_RGBA8888);
*p = SDL_MapRGBA(ctx->pixelFormat, 255, 0, 0, 255);
https://zestedesavoir.com/tutoriels/1014/utiliser-la-sdl-en-langage-c/modification-pixels-par-pixels/
#include <stdbool.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
typedef struct {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *tex;
uint32_t *pixels;
SDL_PixelFormat *pixelFormat;
} Context;
void Cleanup(Context *ctx) {
if (ctx) {
if (ctx->pixelFormat)
free(ctx->pixelFormat);
if (ctx->pixels)
free(ctx->pixels);
if (ctx->tex)
SDL_DestroyTexture(ctx->tex);
if (ctx->renderer)
SDL_DestroyRenderer(ctx->renderer);
if (ctx->window)
SDL_DestroyWindow(ctx->window);
}
}
uint32_t Init(Context *ctx) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Error: %s", SDL_GetError());
return -1;
}
ctx->window =
SDL_CreateWindow("sandbox2", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
if (ctx->window == NULL) {
fprintf(stderr, "Error: %s", SDL_GetError());
return -1;
}
ctx->renderer = SDL_CreateRenderer(
ctx->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ctx->renderer == NULL) {
fprintf(stderr, "Error: %s", SDL_GetError());
return -1;
}
ctx->tex = SDL_CreateTexture(ctx->renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING, 64, 32);
if (ctx->tex == NULL) {
fprintf(stderr, "Error: %s", SDL_GetError());
return -1;
}
ctx->pixelFormat = SDL_AllocFormat(SDL_PIXELFORMAT_RGBA8888);
ctx->pixels = (uint32_t *)malloc(sizeof(uint32_t) * 64 * 32);
SDL_SetRenderDrawColor(ctx->renderer, 225, 240, 200, 255);
return 0;
}
void Loop(void *arg) {
Context *ctx = (Context *)arg;
Uint32 format;
int w;
int h;
SDL_QueryTexture(ctx->tex, &format, NULL, &w, &h);
int pitch;
uint8_t *pixels;
SDL_LockTexture(ctx->tex, NULL, (void **)&pixels, &pitch);
for (int y = 0; y < h; y++) {
Uint32 *p = (Uint32 *)(pixels + pitch*y); // cast for a pointer increments by 4 bytes.(RGBA)
for (int x = 0; x < w; x++) {
// *p = 0x00FF0000;
if (x > 20)
*p = SDL_MapRGBA(ctx->pixelFormat, 255, 0, 0, 255);
else if (x > 10)
*p = SDL_MapRGBA(ctx->pixelFormat, 0, 255, 0, 255);
else
*p = SDL_MapRGBA(ctx->pixelFormat, 0, 0, 255, 255);
p++;
}
}
SDL_UnlockTexture(ctx->tex);
//
// Draw
//
SDL_RenderClear(ctx->renderer);
SDL_RenderCopy(ctx->renderer, ctx->tex, NULL, &(SDL_Rect){0, 0, 64*2, 32*2}); // Scale * 2
SDL_RenderPresent(ctx->renderer);
}
int main() {
Context ctx = {0, 0, 0, 0, 0};
Init(&ctx);
Loop(&ctx);
emscripten_set_main_loop_arg(Loop, &ctx, -1, 1 /*block*/);
printf("don't run this message by block.");
return 0;
}
emcc test_pixel3.c ^
-O2 ^
-Wall -Wextra -pedantic ^
-s USE_SDL=2 ^
-s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS="[""png""]" ^
-s USE_SDL_TTF=2 ^
--preload-file assets ^
-o test_pixel3.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment