Files
proyectos-en-sdl2/palette/main.cpp
Daniel Cortes 242e60ff40 Initial commit
2020-05-22 01:54:52 -04:00

137 lines
4.2 KiB
C++
Executable File

#include <SDL2/SDL.h>
#include <sstream>
#include "engine.hpp"
class Palette : public engine {
private:
int TILE_SIZE = 32;
int MARGIN_TOP = 32;
SDL_Color clicked = {0,0,0, 0};
SDL_Color colors[64] = {
{124,124,124, 0},
{0,0,252, 0},
{0,0,188, 0},
{68,40,188, 0},
{148,0,132, 0},
{168,0,32, 0},
{168,16,0, 0},
{136,20,0, 0},
{80,48,0, 0},
{0,120,0, 0},
{0,104,0, 0},
{0,88,0, 0},
{0,64,88, 0},
{0,0,0, 0},
{0,0,0, 0},
{0,0,0, 0},
{188,188,188, 0},
{0,120,248, 0},
{0,88,248, 0},
{104,68,252, 0},
{216,0,204, 0},
{228,0,88, 0},
{248,56,0, 0},
{228,92,16, 0},
{172,124,0, 0},
{0,184,0, 0},
{0,168,0, 0},
{0,168,68, 0},
{0,136,136, 0},
{0,0,0, 0},
{0,0,0, 0},
{0,0,0, 0},
{248,248,248, 0},
{60,188,252, 0},
{104,136,252, 0},
{152,120,248, 0},
{248,120,248, 0},
{248,88,152, 0},
{248,120,88, 0},
{252,160,68, 0},
{248,184,0, 0},
{184,248,24, 0},
{88,216,84, 0},
{88,248,152, 0},
{0,232,216, 0},
{120,120,120, 0},
{0,0,0, 0},
{0,0,0, 0},
{252,252,252, 0},
{164,228,252, 0},
{184,184,248, 0},
{216,184,248, 0},
{248,184,248, 0},
{248,164,192, 0},
{240,208,176, 0},
{252,224,168, 0},
{248,216,120, 0},
{216,248,120, 0},
{184,248,184, 0},
{184,248,216, 0},
{0,252,252, 0},
{248,216,248, 0},
{0,0,0, 0},
{0,0,0, 0}
};
protected:
virtual bool OnUserCreate() {
return true;
}
virtual bool OnUserUpdate(Uint32 elapsedTime) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return false;
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
if(event.button.button == SDL_BUTTON_LEFT){
int pos_x = event.button.x / TILE_SIZE;
int pos_y = (event.button.y - MARGIN_TOP) / TILE_SIZE;
int index = pos_x + (pos_y * 16);
std::cout << pos_x << ", " << pos_y << ": " << index << std::endl;
clicked = colors[index];
}
}
}
SDL_RenderClear(renderer);
SDL_Rect top = {0, 0, SCREEN_WIDTH, MARGIN_TOP};
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &top);
std::ostringstream clicked_color;
clicked_color << (int) clicked.r << " " << (int) clicked.g << " " << (int) clicked.b;
WriteCentered(clicked_color.str(), SCREEN_WIDTH/2, (MARGIN_TOP/2) - (FONT_HEIGHT), {0, 0, 0}, 2);
int x = 0;
for(int i = MARGIN_TOP; i < (TILE_SIZE * 4) + MARGIN_TOP; i += TILE_SIZE){
for(int j = 0; j < TILE_SIZE * 16; j += TILE_SIZE){
SDL_Rect rect = {j, i, TILE_SIZE, TILE_SIZE};
SDL_SetRenderDrawColor(renderer, colors[x].r, colors[x].g, colors[x].b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &rect);
SDL_Color color;
if((0.2126*colors[x].r + 0.7152*colors[x].g + 0.0722*colors[x].b) > 127){
color = {0, 0, 0};
}else {
color = {255, 255, 255};
}
Write(std::to_string(x), j, i, color, 2);
x++;
}
}
SDL_RenderPresent(renderer);
return true;
}
};
int main(int argc, char* args[]){
Palette palette;
palette.Init("Palette", 32*16, 32*5);
palette.Run();
return 0;
}