Initial commit
This commit is contained in:
22
palette/Makefile
Executable file
22
palette/Makefile
Executable file
@@ -0,0 +1,22 @@
|
||||
CC = g++
|
||||
FILES = main.cpp
|
||||
OBJS = $(FILES:.cpp=.o)
|
||||
OBJS += engine.o
|
||||
CFLAGS = -Wall -Wextra -std=c++17 -O3
|
||||
LFLAGS =
|
||||
|
||||
EXE = run
|
||||
|
||||
CFLAGS += $(shell pkg-config --cflags sdl2)
|
||||
CFLAGS += $(pkg-config --cflags SDL2_image)
|
||||
|
||||
LFLAGS += $(shell pkg-config --libs sdl2)
|
||||
LFLAGS += $(shell pkg-config --libs SDL2_image)
|
||||
|
||||
all: $(EXE)
|
||||
|
||||
$(EXE): $(OBJS)
|
||||
$(CC) $(OBJS) $(LFLAGS) -o $(EXE)
|
||||
|
||||
$(OBJS): $(FILES)
|
||||
$(CC) -c $(FILES) $(CFLAGS)
|
||||
42
palette/engine.hpp
Executable file
42
palette/engine.hpp
Executable file
@@ -0,0 +1,42 @@
|
||||
#ifndef _ENGINE_H
|
||||
#define _ENGINE_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
|
||||
class engine {
|
||||
public:
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *font;
|
||||
SDL_Event event;
|
||||
|
||||
int SCREEN_WIDTH;
|
||||
int SCREEN_HEIGHT;
|
||||
int FONT_WIDTH;
|
||||
int FONT_HEIGHT;
|
||||
|
||||
public:
|
||||
engine();
|
||||
~engine();
|
||||
bool Init(std::string name, int width, int height);
|
||||
void Run();
|
||||
void Write(std::string text, int x, int y, SDL_Color color = SDL_Color{0xff, 0xff, 0xff, 0xff}, int scale = 1);
|
||||
void WriteCentered(std::string text, int x, int y, SDL_Color color = SDL_Color{0xff, 0xff, 0xff, 0xff}, int scale = 1);
|
||||
void WriteAlignRight(std::string text, int x, int y, SDL_Color color = SDL_Color{0xff, 0xff, 0xff, 0xff}, int scale = 1);
|
||||
|
||||
protected:
|
||||
virtual bool OnUserCreate() = 0;
|
||||
virtual bool OnUserUpdate(Uint32 elapsedTime) = 0;
|
||||
|
||||
private:
|
||||
std::string getExecutablePath();
|
||||
std::string getPath(std::string path);
|
||||
bool LoadFontTexture();
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
palette/engine.o
Executable file
BIN
palette/engine.o
Executable file
Binary file not shown.
BIN
palette/font.png
Executable file
BIN
palette/font.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
136
palette/main.cpp
Executable file
136
palette/main.cpp
Executable file
@@ -0,0 +1,136 @@
|
||||
#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;
|
||||
}
|
||||
BIN
palette/main.o
Normal file
BIN
palette/main.o
Normal file
Binary file not shown.
BIN
palette/palette.png
Executable file
BIN
palette/palette.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 1004 B |
BIN
palette/run
Executable file
BIN
palette/run
Executable file
Binary file not shown.
Reference in New Issue
Block a user