From f6210b79c793d27d0bac12a83a18445759e8447c Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Mon, 5 Feb 2024 13:51:34 +0100 Subject: [PATCH] Primer commit --- .gitignore | 1 + Makefile | 9 +++++++++ paleta.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 paleta.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1530978 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.o \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ee5090c --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +name = paleta +executable = $(name).o +source = $(name).cpp + +linux: + g++ $(source) -std=c++11 -Wall -Os -lSDL2 -o "$(executable)" + +macos: + clang++ $(source) -std=c++11 -Wall -Os -lSDL2 -ffunction-sections -fdata-sections -o $(executable) \ No newline at end of file diff --git a/paleta.cpp b/paleta.cpp new file mode 100644 index 0000000..ecd28e6 --- /dev/null +++ b/paleta.cpp @@ -0,0 +1,49 @@ +#include + +struct ARGB +{ + Uint8 b, g, r, a; +}; + +int main(int argc, char *argv[]) +{ + SDL_Init(SDL_INIT_EVERYTHING); + + SDL_Window *w = SDL_CreateWindow("pixels", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); + SDL_Renderer *r = SDL_CreateRenderer(w, -1, 0); + SDL_RenderSetLogicalSize(r, 320, 240); + SDL_Texture *t = SDL_CreateTexture(r, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240); + + ARGB *pixels; + int pitch; + + SDL_Event sdlEvent; + bool exit = false; + while (!exit) + { + while (SDL_PollEvent(&sdlEvent)) + { + if (sdlEvent.type == SDL_QUIT) + { + exit = true; + break; + } + } + + SDL_LockTexture(t, nullptr, (void **)&pixels, &pitch); + + for (int i = 0; i < 76800; ++i) + { + pixels[i].b = rand() % 256; + pixels[i].g = rand() % 256; + pixels[i].r = rand() % 256; + pixels[i].a = 255; + } + + SDL_UnlockTexture(t); + + SDL_RenderCopy(r, t, nullptr, nullptr); + + SDL_RenderPresent(r); + } +} \ No newline at end of file