texture rendering working, but colors are broken

parent 0ef2756c
No preview for this file type
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_scancode.h>
#include <SDL2/SDL_surface.h>
......@@ -80,6 +81,32 @@ calculate_set(Array *arr, int height, int width, ViewInfo view)
}
}
void
update_texture(App *app, Array *arr) {
void *pixels;
int pitch;
if (SDL_LockTexture(app->texture, NULL, &pixels, &pitch) != 0) {
fprintf(stderr, "Failed to lock texture: %s\n", SDL_GetError());
return;
}
uint32_t *dst = pixels;
Color *src = arr->pointer;
for (int y = 0; y < app->win_height; y++) {
for (int x = 0; x < app->win_width; x++) {
Color c = src[x * app->win_height + y];
// Convert RGB to RGBA8888 format
*dst++ = (c.b << 0) | (c.g << 8) | (c.r << 16) | (255 << 24);
}
dst += pitch / sizeof(uint32_t) - app->win_width;
}
SDL_UnlockTexture(app->texture);
}
void
render_cl(Array *arr, App *app, ViewInfo view) {
......@@ -91,18 +118,10 @@ render_cl(Array *arr, App *app, ViewInfo view) {
arr->size = app->win_height * app->win_width * sizeof(Color);
}
calculate_set(arr, app->win_height, app->win_width, view);
update_texture(app, arr);
for (int x = 0; x < app->win_width; x++) {
for (int y = 0; y < app->win_height; y++) {
SDL_SetRenderDrawColor(app->renderer,
arr->pointer[x * app->win_height + y].r,
arr->pointer[x * app->win_height + y].g,
arr->pointer[x * app->win_height + y].b,
255);
SDL_RenderDrawPoint(app->renderer, x, y);
}
}
SDL_RenderCopy(app->renderer, app->texture, NULL, NULL);
}
......@@ -156,6 +175,10 @@ main(void)
SDL_CreateRenderer(app.window,
-1,
SDL_RENDERER_ACCELERATED),
SDL_CreateTexture(app.renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT),
SCREEN_HEIGHT,
SCREEN_WIDTH
};
......
......@@ -2,6 +2,7 @@
#define MANDELBROT_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
......@@ -15,6 +16,7 @@ typedef struct {
typedef struct{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
int win_height;
int win_width;
} App ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment