Custom pixel buffer
This commit is contained in:
parent
968d76e925
commit
2134365ad0
125
main.c
Normal file
125
main.c
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_events.h>
|
||||||
|
#include <SDL2/SDL_rect.h>
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <SDL2/SDL_surface.h>
|
||||||
|
#include <SDL2/SDL_video.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define WINDOW_WIDTH 800
|
||||||
|
#define WINDOW_HEIGHT 600
|
||||||
|
|
||||||
|
#define SURFACE_WIDTH 780
|
||||||
|
#define SURFACE_HEIGHT 580
|
||||||
|
|
||||||
|
#define RMASK 0xff000000
|
||||||
|
#define GMASK 0x00ff0000
|
||||||
|
#define BMASK 0x0000ff00
|
||||||
|
#define AMASK 0x000000ff
|
||||||
|
|
||||||
|
#define WHITE 0xff
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t x;
|
||||||
|
uint32_t y;
|
||||||
|
} coord_t;
|
||||||
|
|
||||||
|
coord_t coord_from_index(uint32_t index, uint32_t w);
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
|
|
||||||
|
SDL_Window *window =
|
||||||
|
SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
|
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
|
|
||||||
|
SDL_Renderer *renderer = SDL_CreateRenderer(
|
||||||
|
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
|
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
SDL_Event event = {0};
|
||||||
|
|
||||||
|
SDL_Surface *surface = SDL_CreateRGBSurface(0, SURFACE_WIDTH, SURFACE_HEIGHT,
|
||||||
|
32, RMASK, GMASK, BMASK, AMASK);
|
||||||
|
|
||||||
|
uint64_t length = surface->h * surface->pitch;
|
||||||
|
|
||||||
|
uint32_t pixels[length];
|
||||||
|
memset(pixels, 0, length);
|
||||||
|
|
||||||
|
coord_t coord = {0};
|
||||||
|
|
||||||
|
uint8_t x = 0;
|
||||||
|
uint8_t y = 0;
|
||||||
|
uint32_t c = 0;
|
||||||
|
|
||||||
|
for (uint64_t i = 0; i < length; ++i) {
|
||||||
|
coord = coord_from_index(i, surface->w);
|
||||||
|
|
||||||
|
x = ((float)coord.x / surface->w) * WHITE;
|
||||||
|
y = ((float)coord.y / surface->h) * WHITE;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
c = (((uint32_t)x) << surface->format->Rshift) |
|
||||||
|
(((uint32_t)y) << surface->format->Gshift) |
|
||||||
|
AMASK;
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
pixels[i] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_LockSurface(surface);
|
||||||
|
|
||||||
|
surface->pixels = (void *)pixels;
|
||||||
|
|
||||||
|
SDL_UnlockSurface(surface);
|
||||||
|
|
||||||
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
|
||||||
|
SDL_Rect dest = {
|
||||||
|
.w = surface->w,
|
||||||
|
.h = surface->h,
|
||||||
|
.x = (WINDOW_WIDTH - surface->w) / 2,
|
||||||
|
.y = (WINDOW_HEIGHT - surface->h) / 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
while (SDL_PollEvent(&event)) {
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_QUIT:
|
||||||
|
running = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 16, 22, 25, 255);
|
||||||
|
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
SDL_RenderCopy(renderer, texture, NULL, &dest);
|
||||||
|
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
coord_t coord_from_index(uint32_t index, uint32_t w) {
|
||||||
|
coord_t out = {0};
|
||||||
|
|
||||||
|
out.x = index % w;
|
||||||
|
|
||||||
|
out.y = (index - (index % w)) / w;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user