Initial commit
This commit is contained in:
1
engine
Submodule
1
engine
Submodule
Submodule engine added at f54f46cf18
8
fez/.idea/fez.iml
generated
Normal file
8
fez/.idea/fez.iml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="CPP_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
fez/.idea/modules.xml
generated
Normal file
8
fez/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/fez.iml" filepath="$PROJECT_DIR$/.idea/fez.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
42
fez/.idea/workspace.xml
generated
Normal file
42
fez/.idea/workspace.xml
generated
Normal file
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeRunConfigurationManager" shouldGenerate="true" shouldDeleteObsolete="true">
|
||||
<generated />
|
||||
</component>
|
||||
<component name="CMakeSettings">
|
||||
<configurations>
|
||||
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
|
||||
</configurations>
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="63cc51bf-7306-4be9-84bf-deb18a76e657" name="Default Changelist" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="ClangdSettings">
|
||||
<option name="formatViaClangd" value="false" />
|
||||
</component>
|
||||
<component name="ProjectId" id="1asAs4QiSxuQNUqTz1yzVCMMHBB" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
||||
<property name="WebServerToolWindowFactoryState" value="false" />
|
||||
<property name="cf.first.check.clang-format" value="false" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="SvnConfiguration">
|
||||
<configuration />
|
||||
</component>
|
||||
<component name="WindowStateProjectService">
|
||||
<state x="418" y="190" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1587514181529">
|
||||
<screen x="10" y="10" width="1900" height="1045" />
|
||||
</state>
|
||||
<state x="418" y="190" key="#com.intellij.execution.impl.EditConfigurationsDialog/10.10.1900.1045@10.10.1900.1045" timestamp="1587514181529" />
|
||||
</component>
|
||||
</project>
|
||||
25
fez/Makefile
Normal file
25
fez/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
CC = g++
|
||||
FILES = main.cpp
|
||||
OBJS = $(FILES:.cpp=.o)
|
||||
CFLAGS = -Wall -Wextra -std=c++17 -g
|
||||
|
||||
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)
|
||||
|
||||
clean:
|
||||
rm *.o $(EXE)
|
||||
104
fez/engine.hpp
Normal file
104
fez/engine.hpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#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;
|
||||
std::string title;
|
||||
|
||||
|
||||
public:
|
||||
engine(){}
|
||||
|
||||
~engine() {
|
||||
SDL_DestroyWindow(window);
|
||||
window = nullptr;
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
renderer = nullptr;
|
||||
|
||||
SDL_DestroyTexture(font);
|
||||
font = nullptr;
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
bool Init(std::string title, int width, int height) {
|
||||
this->SCREEN_WIDTH = width;
|
||||
this->SCREEN_HEIGHT = height;
|
||||
this->title = title;
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
||||
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow(title.append(" | FPS: 0").c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
|
||||
if(window == nullptr) {
|
||||
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if(renderer == nullptr) {
|
||||
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Run() {
|
||||
bool running = true;
|
||||
|
||||
if(!OnUserCreate()){
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned long acumulatedTime = 0;
|
||||
unsigned long tick1 = SDL_GetTicks();
|
||||
unsigned long tick2 = SDL_GetTicks();
|
||||
unsigned long fpsAcumulator = 0;
|
||||
unsigned long fps = 0;
|
||||
|
||||
while(running){
|
||||
tick2 = SDL_GetTicks();
|
||||
Uint32 elapsedTime = tick2 - tick1;
|
||||
tick1 = tick2;
|
||||
|
||||
fpsAcumulator++;
|
||||
acumulatedTime += elapsedTime;
|
||||
if(acumulatedTime >= 1000) {
|
||||
acumulatedTime -= 1000;
|
||||
fps = fpsAcumulator;
|
||||
fpsAcumulator = 0;
|
||||
|
||||
std::ostringstream title;
|
||||
title << this->title << " | FPS: " << fps;
|
||||
SDL_SetWindowTitle(window, title.str().c_str());
|
||||
}
|
||||
|
||||
running = OnUserUpdate(elapsedTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
virtual bool OnUserCreate() = 0;
|
||||
virtual bool OnUserUpdate(Uint32 elapsedTime) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
160
fez/main.cpp
Normal file
160
fez/main.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "engine.hpp"
|
||||
|
||||
struct symbol {
|
||||
SDL_Texture* texture;
|
||||
std::string repr;
|
||||
};
|
||||
|
||||
class Main : public engine {
|
||||
private:
|
||||
std::vector<symbol> letters;
|
||||
std::vector<symbol> numbers;
|
||||
std::vector<symbol> controls;
|
||||
|
||||
SDL_Texture* createTexture(unsigned long pattern) {
|
||||
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 5, 5);
|
||||
std::vector<unsigned char> pixels(5 * 5 * 4, 0);
|
||||
|
||||
for(int i = 0; i < 25; i++) {
|
||||
unsigned int offset = (5 * 4 * (i / 5)) + ((i % 5) * 4);
|
||||
if((pattern >> i) & 1) {
|
||||
pixels[offset + 0] = 0x00;
|
||||
pixels[offset + 1] = 0x00;
|
||||
pixels[offset + 2] = 0x00;
|
||||
pixels[offset + 3] = SDL_ALPHA_OPAQUE;
|
||||
} else{
|
||||
pixels[offset + 0] = 0xff;
|
||||
pixels[offset + 1] = 0xff;
|
||||
pixels[offset + 2] = 0xff;
|
||||
pixels[offset + 3] = SDL_ALPHA_OPAQUE;
|
||||
}
|
||||
}
|
||||
SDL_UpdateTexture(texture, NULL, &pixels[0], 5 * 4);
|
||||
return texture;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool OnUserCreate() {
|
||||
letters = {
|
||||
{createTexture(33539231), "A"},
|
||||
{createTexture(33052223), "B"},
|
||||
{createTexture(32759039), "C"},
|
||||
{createTexture(31358975), "D"},
|
||||
{createTexture(32569247), "E"},
|
||||
{createTexture(33211071), "F"},
|
||||
{createTexture(24768119), "G"},
|
||||
{createTexture(24696383), "H"},
|
||||
{createTexture(33553973), "I"},
|
||||
{createTexture(24379391), "J"},
|
||||
{createTexture(30990071), "K"},
|
||||
{createTexture(28896831), "L"},
|
||||
{createTexture(32655359), "M"},
|
||||
{createTexture(33084479), "N"},
|
||||
{createTexture(33453983), "O"},
|
||||
{createTexture(33553655), "P"},
|
||||
{createTexture(33195711), "R"},
|
||||
{createTexture(31256381), "S"},
|
||||
{createTexture(33084989), "T"},
|
||||
{createTexture(22609919), "V"},
|
||||
{createTexture(33554333), "W"},
|
||||
{createTexture(31389239), "X"},
|
||||
{createTexture(33091131), "Y"},
|
||||
{createTexture(32759327), "Z"},
|
||||
{createTexture(33062912), " "}
|
||||
};
|
||||
numbers = {
|
||||
{createTexture(33080895), "0"},
|
||||
{createTexture(33085119), "1"},
|
||||
{createTexture(33093183), "2"},
|
||||
{createTexture(33216063), "3"},
|
||||
{createTexture(33093311), "3"},
|
||||
{createTexture(33087039), "4"},
|
||||
{createTexture(33216191), "4"},
|
||||
{createTexture(33087167), "5"},
|
||||
{createTexture(33224255), "5"},
|
||||
{createTexture(33095231), "6"},
|
||||
{createTexture(33224383), "6"},
|
||||
{createTexture(33218111), "7"},
|
||||
{createTexture(33095359), "7"},
|
||||
{createTexture(33218239), "8"},
|
||||
{createTexture(33226303), "9"},
|
||||
{createTexture(33226431), "10"}
|
||||
};
|
||||
controls = {
|
||||
{createTexture(143488), "UP"},
|
||||
{createTexture(137344), "DOWN"},
|
||||
{createTexture(14464), "LEFT"},
|
||||
{createTexture(145408), "RIGHT"},
|
||||
{createTexture(71808), "LT"},
|
||||
{createTexture(274560), "RT"},
|
||||
{createTexture(405504), "BUTTON A"},
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
void drawLetters() {
|
||||
int size = 35;
|
||||
int offset_x = 20;
|
||||
int offset_y = 20;
|
||||
int margin = 5;
|
||||
|
||||
for(size_t i = 0; i < letters.size(); i++) {
|
||||
int row = i % 5;
|
||||
int col = i / 5;
|
||||
|
||||
int x = offset_x + (row * size) + (margin * row);
|
||||
int y = offset_y + (col * size) + (margin * col);
|
||||
|
||||
SDL_Rect rect = {x, y, size, size};
|
||||
SDL_RenderCopy(renderer, letters[i].texture, nullptr, &rect);
|
||||
}
|
||||
}
|
||||
|
||||
int overLetter(int x, int y){
|
||||
int size = 35;
|
||||
int offset_x = 20;
|
||||
int offset_y = 20;
|
||||
int margin = 5;
|
||||
|
||||
for(size_t i = 0; i < letters.size(); i++) {
|
||||
int row = i % 5;
|
||||
int col = i / 5;
|
||||
int letter_x = offset_x + (row * size) + (margin * row);
|
||||
int letter_y = offset_y + (col * size) + (margin * col);
|
||||
|
||||
if((x >= letter_x && x <= letter_x + size) && (y >= letter_y && y <= letter_y + size)){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
virtual bool OnUserUpdate(Uint32 elapsedTime) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if(event.type == SDL_QUIT) return false;
|
||||
if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
|
||||
int i = overLetter(event.button.x, event.button.y);
|
||||
if(i >= 0){
|
||||
std::cout << "Over Letter: " << letters[i].repr << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderClear(renderer);
|
||||
drawLetters();
|
||||
SDL_RenderPresent(renderer);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Main main;
|
||||
main.Init("Test", 640, 480);
|
||||
main.Run();
|
||||
return 0;
|
||||
}
|
||||
BIN
fez/main.o
Normal file
BIN
fez/main.o
Normal file
Binary file not shown.
55
fez/paterns
Normal file
55
fez/paterns
Normal file
@@ -0,0 +1,55 @@
|
||||
Letters:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
0
|
||||
|
||||
Numbers:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Controls:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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.
11
rectangles/Makefile
Executable file
11
rectangles/Makefile
Executable file
@@ -0,0 +1,11 @@
|
||||
OBJS = main.cpp engine.h
|
||||
CC = g++
|
||||
COMPILER_FLAGS = -Wall
|
||||
LINKER_FLAGS = -lSDL2 -lSDL2_image
|
||||
OBJ_NAME = run
|
||||
|
||||
all: $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
|
||||
clean:
|
||||
rm *.o $(OBJ_NAME)
|
||||
168
rectangles/engine.h
Executable file
168
rectangles/engine.h
Executable file
@@ -0,0 +1,168 @@
|
||||
#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() {
|
||||
SDL_DestroyWindow(window);
|
||||
window = nullptr;
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
renderer = nullptr;
|
||||
|
||||
SDL_DestroyTexture(font);
|
||||
font = nullptr;
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
|
||||
bool Init(std::string name, int width, int height) {
|
||||
SCREEN_WIDTH = width;
|
||||
SCREEN_HEIGHT = height;
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
||||
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
|
||||
if(window == nullptr) {
|
||||
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
if(renderer == nullptr) {
|
||||
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
|
||||
std::cout << "IMG_Init Error: " << IMG_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!LoadFontTexture()){
|
||||
std::cout << "LoadFontTexture Error" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
FONT_WIDTH = 6;
|
||||
FONT_HEIGHT = 10;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Run() {
|
||||
bool running = true;
|
||||
|
||||
if(!OnUserCreate()){
|
||||
return;
|
||||
}
|
||||
|
||||
Uint32 tp1 = SDL_GetTicks();
|
||||
Uint32 tp2 = SDL_GetTicks();
|
||||
while(running){
|
||||
tp2 = SDL_GetTicks();
|
||||
Uint32 elapsedTime = tp2 - tp1;
|
||||
tp1 = tp2;
|
||||
|
||||
running = OnUserUpdate(elapsedTime);
|
||||
}
|
||||
}
|
||||
|
||||
void Write(std::string text, int x, int y, SDL_Color color = {0xff, 0xff, 0xff}, int scale = 1) {
|
||||
SDL_SetTextureColorMod(font, color.r, color.g, color.b);
|
||||
int i = x;
|
||||
for(char &c: text) {
|
||||
SDL_Rect screen_rect = {i, y, FONT_WIDTH * scale, FONT_HEIGHT * scale};
|
||||
SDL_Rect font_rect;
|
||||
font_rect.y = 0;
|
||||
font_rect.w = FONT_WIDTH;
|
||||
font_rect.h = FONT_HEIGHT;
|
||||
|
||||
if(c >= 65 && c <= 90) {
|
||||
font_rect.x = 6 * (c - 'A');
|
||||
}else if (c >= 48 && c <= 57) {
|
||||
font_rect.x = 6 * (c - '0' + 26);
|
||||
}else if (c == ' ') {
|
||||
font_rect.x = 216;
|
||||
}
|
||||
|
||||
SDL_RenderCopy(renderer, font, &font_rect, &screen_rect);
|
||||
|
||||
i += 6*scale;
|
||||
}
|
||||
}
|
||||
|
||||
void WriteCentered(std::string text, int x, int y, SDL_Color color = {0xff, 0xff, 0xff}, int scale = 1) {
|
||||
int width = FONT_WIDTH * text.length() * scale;
|
||||
x -= width / 2;
|
||||
Write(text, x, y, color, scale);
|
||||
}
|
||||
|
||||
void WriteAlignRight(std::string text, int x, int y, SDL_Color color = {0xff, 0xff, 0xff}, int scale = 1) {
|
||||
int width = FONT_WIDTH * text.length() * scale;
|
||||
x -= width;
|
||||
Write(text, x, y, color, scale);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool OnUserCreate() = 0;
|
||||
virtual bool OnUserUpdate(Uint32 elapsedTime) = 0;
|
||||
|
||||
private:
|
||||
std::string getExecutablePath() {
|
||||
char buf[PATH_MAX];
|
||||
ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf)-1);
|
||||
if (len != -1) {
|
||||
buf[len] = '\0';
|
||||
}
|
||||
std::string path = buf;
|
||||
return path.substr(0, path.find_last_of('/') + 1);
|
||||
}
|
||||
|
||||
std::string getPath(std::string path){
|
||||
return getExecutablePath() + path;
|
||||
}
|
||||
|
||||
bool LoadFontTexture(){
|
||||
SDL_Surface *surface = IMG_Load(getPath("font.png").c_str());
|
||||
if(surface == nullptr) {
|
||||
std::cout << "IMG_Load Error: " << IMG_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
font = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if(font == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
rectangles/font.png
Executable file
BIN
rectangles/font.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
44
rectangles/main.cpp
Executable file
44
rectangles/main.cpp
Executable file
@@ -0,0 +1,44 @@
|
||||
#include <cstdlib>
|
||||
#include <time.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "engine.h"
|
||||
|
||||
class Game : public engine {
|
||||
protected:
|
||||
|
||||
virtual bool OnUserCreate() {
|
||||
srand(time(0));
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool OnUserUpdate(Uint32 elapsedTime) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
|
||||
|
||||
SDL_Color color = {0, 64, 88};
|
||||
Write("HOLA", SCREEN_WIDTH/2, FONT_HEIGHT * 0, color, 2);
|
||||
WriteCentered("HOLA", SCREEN_WIDTH/2, FONT_HEIGHT * 1, color, 2);
|
||||
WriteAlignRight("HOLA", SCREEN_WIDTH/2, FONT_HEIGHT * 2, color, 2);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main(int argc, char* args[]){
|
||||
Game game;
|
||||
game.Init("Game", 640, 480);
|
||||
game.Run();
|
||||
return 0;
|
||||
}
|
||||
BIN
rectangles/run
Executable file
BIN
rectangles/run
Executable file
Binary file not shown.
15
relearn/Makefile
Executable file
15
relearn/Makefile
Executable file
@@ -0,0 +1,15 @@
|
||||
CXX = g++
|
||||
CXXFLAGS = -Wall -c -std=c++11
|
||||
LDFLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf
|
||||
EXE = main
|
||||
|
||||
all: $(EXE)
|
||||
|
||||
$(EXE): main.o
|
||||
$(CXX) $< $(LDFLAGS) -o $@
|
||||
|
||||
main.o: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
rm *.o && rm $(EXE)
|
||||
BIN
relearn/main
Executable file
BIN
relearn/main
Executable file
Binary file not shown.
108
relearn/main.cpp
Executable file
108
relearn/main.cpp
Executable file
@@ -0,0 +1,108 @@
|
||||
#include <iostream>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void cleanup(T *t, Args&&... args)
|
||||
{
|
||||
cleanup(t);
|
||||
cleanup(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<> inline void cleanup<SDL_Window>(SDL_Window *win) { if(!win){return;} SDL_DestroyWindow(win); }
|
||||
template<> inline void cleanup<SDL_Renderer>(SDL_Renderer *ren) { if(!ren){return;}SDL_DestroyRenderer(ren); }
|
||||
template<> inline void cleanup<SDL_Texture>(SDL_Texture *tex) { if(!tex){return;} SDL_DestroyTexture(tex); }
|
||||
template<> inline void cleanup<SDL_Surface>(SDL_Surface *surf) { if(!surf){return;} SDL_FreeSurface(surf); }
|
||||
template<> inline void cleanup<TTF_Font>(TTF_Font *font) { if(!font){return;} TTF_CloseFont(font); }
|
||||
|
||||
|
||||
void logError(const std::string &msg)
|
||||
{
|
||||
std::cout << msg << " error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren)
|
||||
{
|
||||
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
|
||||
if (texture == nullptr) logError("LoadTexture");
|
||||
return texture;
|
||||
}
|
||||
|
||||
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst, SDL_Rect *clip = nullptr)
|
||||
{
|
||||
SDL_RenderCopy(ren, tex, clip, &dst);
|
||||
}
|
||||
|
||||
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, SDL_Rect *clip = nullptr)
|
||||
{
|
||||
SDL_Rect dst;
|
||||
dst.x = x;
|
||||
dst.y = y;
|
||||
if (clip != nullptr) {
|
||||
dst.w = clip->w;
|
||||
dst.h = clip->h;
|
||||
} else {
|
||||
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
|
||||
}
|
||||
renderTexture(tex, ren, dst, clip);
|
||||
}
|
||||
|
||||
SDL_Texture* renderText(const std::string &message, const std::string &file,
|
||||
SDL_Color color, int size, SDL_Renderer *renderer)
|
||||
{
|
||||
TTF_Font *font = TTF_OpenFont(file.c_str(), size);
|
||||
if(font == nullptr) { logError("TTF_OpenFont"); return nullptr; }
|
||||
SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
|
||||
if(surf == nullptr) { cleanup(font); logError("TTF_RenderText"); return nullptr; }
|
||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
|
||||
if(texture == nullptr) { cleanup(surf, font); logError("SDL_CreateTextureFromSurface"); return nullptr; }
|
||||
|
||||
cleanup(surf, font);
|
||||
return texture;
|
||||
}
|
||||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
const int TILE_SIZE = 40;
|
||||
|
||||
int main(int, char**) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) { logError("SDL_Init"); return 1; }
|
||||
if (TTF_Init() != 0) { logError("TTF_Init"); SDL_Quit(); return 1; }
|
||||
|
||||
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
if (win == nullptr) { logError("CreateWindow"); SDL_Quit(); return 1;}
|
||||
|
||||
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
if (ren == nullptr) { cleanup(win); logError("CreateRenderer"); SDL_Quit(); return 1; }
|
||||
|
||||
SDL_Color color = {255, 255, 255, 255};
|
||||
SDL_Texture *image = renderText("TTF Font Test :3", "worksans.ttf", color, 64, ren);
|
||||
if(image == nullptr) {
|
||||
cleanup(ren, win);
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int iw, ih;
|
||||
SDL_QueryTexture(image, NULL, NULL, &iw, &ih);
|
||||
int x = SCREEN_WIDTH / 2 - iw / 2;
|
||||
int y = SCREEN_HEIGHT / 2 - ih / 2;
|
||||
|
||||
SDL_Event e;
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
while(SDL_PollEvent(&e)) {
|
||||
if(e.type == SDL_QUIT) { quit = true; }
|
||||
}
|
||||
SDL_RenderClear(ren);
|
||||
renderTexture(image, ren, x, y);
|
||||
SDL_RenderPresent(ren);
|
||||
}
|
||||
|
||||
cleanup(ren, win);
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
BIN
relearn/main.o
Executable file
BIN
relearn/main.o
Executable file
Binary file not shown.
BIN
relearn/worksans.ttf
Executable file
BIN
relearn/worksans.ttf
Executable file
Binary file not shown.
1
snake
Submodule
1
snake
Submodule
Submodule snake added at 08b500f502
25
snake2/Makefile
Executable file
25
snake2/Makefile
Executable file
@@ -0,0 +1,25 @@
|
||||
CC = g++
|
||||
FILES = game.cpp renderer.cpp events.cpp main.cpp
|
||||
OBJS = $(FILES:.cpp=.o)
|
||||
CFLAGS = -Wall -Wextra -std=c++17 -O3 -pedantic
|
||||
LFLAGS =
|
||||
|
||||
EXE = snake
|
||||
|
||||
CFLAGS += $(shell pkg-config --cflags sdl2)
|
||||
CFLAGS += $(shell 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)
|
||||
|
||||
|
||||
clean:
|
||||
rm *.o $(EXE)
|
||||
39
snake2/events.cpp
Executable file
39
snake2/events.cpp
Executable file
@@ -0,0 +1,39 @@
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "game.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "events.hpp"
|
||||
|
||||
Events::Events(Game *game, Renderer *renderer): game(game), renderer(renderer) {}
|
||||
|
||||
bool Events::process()
|
||||
{
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if(event.type == SDL_QUIT) {
|
||||
return false;
|
||||
} else if (event.type == SDL_KEYDOWN) {
|
||||
switch(event.key.keysym.sym) {
|
||||
case SDLK_LEFT:
|
||||
game->snake.turn(DIR_LEFT);
|
||||
break;
|
||||
case SDLK_UP:
|
||||
game->snake.turn(DIR_UP);
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
game->snake.turn(DIR_RIGHT);
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
game->snake.turn(DIR_DOWN);
|
||||
break;
|
||||
case SDLK_r:
|
||||
game->reset();
|
||||
break;
|
||||
}
|
||||
} else if (event.type == SDL_WINDOWEVENT) {
|
||||
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||
renderer->updateSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
19
snake2/events.hpp
Executable file
19
snake2/events.hpp
Executable file
@@ -0,0 +1,19 @@
|
||||
#ifndef EVENTS_H
|
||||
#define EVENTS_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "game.hpp"
|
||||
#include "renderer.hpp"
|
||||
|
||||
class Events
|
||||
{
|
||||
private:
|
||||
SDL_Event event;
|
||||
Game *game;
|
||||
Renderer *renderer;
|
||||
public:
|
||||
Events(Game *game, Renderer *renderer);
|
||||
bool process();
|
||||
};
|
||||
|
||||
#endif
|
||||
137
snake2/game.cpp
Executable file
137
snake2/game.cpp
Executable file
@@ -0,0 +1,137 @@
|
||||
#include "game.hpp"
|
||||
#include "random.hpp"
|
||||
|
||||
Food::Food(): max_x(0), max_y(0) {}
|
||||
|
||||
Food::Food(int max_x, int max_y): max_x(max_x), max_y(max_y)
|
||||
{
|
||||
relocate();
|
||||
}
|
||||
|
||||
void Food::relocate()
|
||||
{
|
||||
x = random_generator() % max_x;
|
||||
y = random_generator() % max_y;
|
||||
}
|
||||
|
||||
Snake::Snake()
|
||||
{
|
||||
body.push_back({0, 0});
|
||||
last_direction = DIR_LEFT;
|
||||
direction = DIR_LEFT;
|
||||
alive = true;
|
||||
}
|
||||
|
||||
Snake::Snake(int start_x, int start_y)
|
||||
{
|
||||
body.push_back({start_x, start_y});
|
||||
direction = DIR_LEFT;
|
||||
alive = true;
|
||||
}
|
||||
|
||||
void Snake::turn(Direction direction)
|
||||
{
|
||||
switch(direction)
|
||||
{
|
||||
case DIR_UP : if(last_direction != DIR_DOWN) this->direction = DIR_UP ; break;
|
||||
case DIR_DOWN : if(last_direction != DIR_UP) this->direction = DIR_DOWN ; break;
|
||||
case DIR_RIGHT: if(last_direction != DIR_LEFT) this->direction = DIR_RIGHT; break;
|
||||
case DIR_LEFT : if(last_direction != DIR_RIGHT) this->direction = DIR_LEFT ; break;
|
||||
}
|
||||
}
|
||||
|
||||
void Snake::move()
|
||||
{
|
||||
if(!alive) return;
|
||||
|
||||
int x = body.front().x;
|
||||
int y = body.front().y;
|
||||
|
||||
switch(direction){
|
||||
case DIR_UP: body.push_front({x, y - 1}); break;
|
||||
case DIR_DOWN: body.push_front({x, y + 1}); break;
|
||||
case DIR_LEFT: body.push_front({x - 1, y}); break;
|
||||
case DIR_RIGHT: body.push_front({x + 1, y}); break;
|
||||
}
|
||||
|
||||
last_direction = direction;
|
||||
|
||||
body.pop_back();
|
||||
}
|
||||
|
||||
void Snake::eat()
|
||||
{
|
||||
int x = body.back().x;
|
||||
int y = body.back().y;
|
||||
|
||||
body.push_back({x, y});
|
||||
}
|
||||
|
||||
Game::Game(int tiles_x, int tiles_y) : tiles_x(tiles_x), tiles_y(tiles_y)
|
||||
{
|
||||
dificulty_multiplier = 0;
|
||||
points = 0;
|
||||
|
||||
food = Food(tiles_x, tiles_y);
|
||||
snake = Snake(tiles_x/2, tiles_y/2);
|
||||
}
|
||||
|
||||
void Game::update()
|
||||
{
|
||||
snake.move();
|
||||
Body head = snake.body.front();
|
||||
|
||||
//Check if snake was killed by the walls
|
||||
if(head.x >= tiles_x || head.x < 0 || head.y >= tiles_y || head.y < 0)
|
||||
{
|
||||
snake.alive = false;
|
||||
}
|
||||
|
||||
//Check if snake was killed by itself
|
||||
for(std::list<Body>::iterator b = snake.body.begin(); b != snake.body.end(); b++)
|
||||
{
|
||||
if(b != snake.body.begin() && b->x == head.x && b->y == head.y)
|
||||
{
|
||||
snake.alive = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Check if snake eated some food
|
||||
if(head.x == food.x && head.y == food.y)
|
||||
{
|
||||
snake.eat();
|
||||
|
||||
//Update points and dificulty
|
||||
points++;
|
||||
if(points % 20 == 0)
|
||||
{
|
||||
dificulty_multiplier++;
|
||||
}
|
||||
|
||||
//Move the food to a valid place
|
||||
while(true)
|
||||
{
|
||||
food.relocate();
|
||||
bool intersects = false;
|
||||
for(auto body_part: snake.body)
|
||||
{
|
||||
if(food.x == body_part.x && food.y == body_part.y)
|
||||
{
|
||||
intersects = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!intersects) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::reset()
|
||||
{
|
||||
snake = Snake(tiles_x/2, tiles_y/2);
|
||||
food = Food(tiles_x, tiles_y);
|
||||
dificulty_multiplier = 1;
|
||||
points = 0;
|
||||
}
|
||||
|
||||
63
snake2/game.hpp
Executable file
63
snake2/game.hpp
Executable file
@@ -0,0 +1,63 @@
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include <list>
|
||||
|
||||
enum Direction { DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT };
|
||||
|
||||
class Food
|
||||
{
|
||||
private:
|
||||
int max_x;
|
||||
int max_y;
|
||||
public:
|
||||
int x;
|
||||
int y;
|
||||
|
||||
Food();
|
||||
Food(int max_x, int max_y);
|
||||
void relocate();
|
||||
};
|
||||
|
||||
|
||||
struct Body
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
class Snake
|
||||
{
|
||||
public:
|
||||
Direction last_direction;
|
||||
Direction direction;
|
||||
std::list<Body> body;
|
||||
bool alive;
|
||||
|
||||
Snake();
|
||||
Snake(int start_x, int start_y);
|
||||
|
||||
void move();
|
||||
void turn(Direction direction);
|
||||
void eat();
|
||||
};
|
||||
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
Snake snake;
|
||||
Food food;
|
||||
|
||||
int tiles_x;
|
||||
int tiles_y;
|
||||
|
||||
int dificulty_multiplier;
|
||||
int points;
|
||||
|
||||
Game(int tiles_x, int tiles_y);
|
||||
|
||||
void update();
|
||||
void reset();
|
||||
};
|
||||
|
||||
#endif
|
||||
42
snake2/main.cpp
Executable file
42
snake2/main.cpp
Executable file
@@ -0,0 +1,42 @@
|
||||
#include <chrono>
|
||||
|
||||
#include "game.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "events.hpp"
|
||||
|
||||
Game game(20, 20);
|
||||
Renderer renderer("snake", 600, 600, 20, 20);
|
||||
Events events(&game, &renderer);
|
||||
|
||||
bool running = true;
|
||||
auto tp1 = std::chrono::system_clock::now();
|
||||
auto tp2 = std::chrono::system_clock::now();
|
||||
std::chrono::duration<float> elapsed_time = tp2 - tp1;
|
||||
float acumulated_time = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
while(running)
|
||||
{
|
||||
tp2 = std::chrono::system_clock::now();
|
||||
elapsed_time = tp2 - tp1;
|
||||
acumulated_time += elapsed_time.count();
|
||||
tp1 = tp2;
|
||||
|
||||
running = events.process();
|
||||
|
||||
if(acumulated_time >= 0.1)
|
||||
{
|
||||
game.update();
|
||||
renderer.clear();
|
||||
renderer.renderArena();
|
||||
renderer.renderSnake(game.snake);
|
||||
renderer.renderFood(game.food);
|
||||
renderer.render();
|
||||
|
||||
acumulated_time = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
11
snake2/random.hpp
Executable file
11
snake2/random.hpp
Executable file
@@ -0,0 +1,11 @@
|
||||
#ifndef RANDOM_H
|
||||
#define RANDOM_H
|
||||
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
|
||||
|
||||
std::minstd_rand0 random_generator(seed);
|
||||
|
||||
#endif
|
||||
96
snake2/renderer.cpp
Executable file
96
snake2/renderer.cpp
Executable file
@@ -0,0 +1,96 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
#include "renderer.hpp"
|
||||
#include "game.hpp"
|
||||
|
||||
const SDL_Color BACKGROUND_COLOR = {0, 64, 88, SDL_ALPHA_OPAQUE};
|
||||
const SDL_Color SNAKE_COLOR = {0, 184, 0, SDL_ALPHA_OPAQUE};
|
||||
const SDL_Color FOOD_COLOR = {248, 56, 0, SDL_ALPHA_OPAQUE};
|
||||
|
||||
Renderer::Renderer(std::string name, int screen_width, int screen_height, int tiles_x, int tiles_y) :
|
||||
screen_width(screen_width),
|
||||
screen_height(screen_height),
|
||||
tiles_x(tiles_x),
|
||||
tiles_y(tiles_y)
|
||||
{
|
||||
tile_size = std::min(screen_width/tiles_x, screen_height/tiles_y);
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
||||
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
||||
if(window == nullptr) {
|
||||
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
if(renderer == nullptr) {
|
||||
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Renderer::~Renderer()
|
||||
{
|
||||
SDL_DestroyWindow(window);
|
||||
window = nullptr;
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
renderer = nullptr;
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void Renderer::updateSize()
|
||||
{
|
||||
SDL_GetWindowSize(window, &screen_width, &screen_height);
|
||||
tile_size = std::min(screen_width/tiles_x, screen_height/tiles_y);
|
||||
}
|
||||
|
||||
void Renderer::clear()
|
||||
{
|
||||
SDL_RenderClear(renderer);
|
||||
}
|
||||
|
||||
void Renderer::renderSnake(Snake snake)
|
||||
{
|
||||
for(auto body: snake.body){
|
||||
SDL_Rect snake_rect = getRectInScreen(body.x, body.y);
|
||||
SDL_SetRenderDrawColor(renderer, SNAKE_COLOR.r, SNAKE_COLOR.g, SNAKE_COLOR.b, SNAKE_COLOR.a);
|
||||
SDL_RenderFillRect(renderer, &snake_rect);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::renderFood(Food food)
|
||||
{
|
||||
SDL_Rect food_rect = getRectInScreen(food.x, food.y);
|
||||
SDL_SetRenderDrawColor(renderer, FOOD_COLOR.r, FOOD_COLOR.g, FOOD_COLOR.b, FOOD_COLOR.a);
|
||||
SDL_RenderFillRect(renderer, &food_rect);
|
||||
}
|
||||
|
||||
void Renderer::renderArena()
|
||||
{
|
||||
SDL_Rect background_rect = {0, 0, screen_width, screen_height};
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderFillRect(renderer, &background_rect);
|
||||
|
||||
SDL_Rect arena_rect = getRectInScreen(0,0);
|
||||
arena_rect.w = tile_size * tiles_x;
|
||||
arena_rect.h = tile_size * tiles_y;
|
||||
SDL_SetRenderDrawColor(renderer, BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b, BACKGROUND_COLOR.a);
|
||||
SDL_RenderFillRect(renderer, &arena_rect);
|
||||
}
|
||||
|
||||
void Renderer::render()
|
||||
{
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
SDL_Rect Renderer::getRectInScreen(int x, int y){
|
||||
int rect_x = (x * tile_size) + ((screen_width / 2) - (tile_size * tiles_x / 2));
|
||||
int rect_y = (y * tile_size) + ((screen_height / 2) - (tile_size * tiles_y / 2));
|
||||
|
||||
SDL_Rect rect = {rect_x, rect_y, tile_size, tile_size};
|
||||
return rect;
|
||||
}
|
||||
37
snake2/renderer.hpp
Executable file
37
snake2/renderer.hpp
Executable file
@@ -0,0 +1,37 @@
|
||||
#ifndef RENDER_H
|
||||
#define RENDER_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
#include "game.hpp"
|
||||
|
||||
|
||||
class Renderer
|
||||
{
|
||||
private:
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Event event;
|
||||
|
||||
int screen_width;
|
||||
int screen_height;
|
||||
|
||||
int tile_size;
|
||||
int tiles_x;
|
||||
int tiles_y;
|
||||
|
||||
SDL_Rect getRectInScreen(int x, int y);
|
||||
|
||||
public:
|
||||
Renderer(std::string name, int screen_width, int screen_height, int tiles_x, int tiles_y);
|
||||
~Renderer();
|
||||
|
||||
void updateSize();
|
||||
void clear();
|
||||
void renderSnake(Snake snake);
|
||||
void renderFood(Food food);
|
||||
void renderArena();
|
||||
void render();
|
||||
};
|
||||
|
||||
#endif
|
||||
25
test/Makefile
Normal file
25
test/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
CC = g++
|
||||
FILES = main.cpp
|
||||
OBJS = $(FILES:.cpp=.o)
|
||||
CFLAGS = -Wall -Wextra -std=c++17 -g
|
||||
|
||||
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)
|
||||
|
||||
clean:
|
||||
rm *.o $(EXE)
|
||||
113
test/main.cpp
Normal file
113
test/main.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
SDL_Init( SDL_INIT_EVERYTHING );
|
||||
atexit( SDL_Quit );
|
||||
|
||||
SDL_Window* window = SDL_CreateWindow
|
||||
(
|
||||
"SDL2",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
600, 600,
|
||||
SDL_WINDOW_SHOWN
|
||||
);
|
||||
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer
|
||||
(
|
||||
window,
|
||||
-1,
|
||||
SDL_RENDERER_ACCELERATED
|
||||
);
|
||||
|
||||
SDL_RendererInfo info;
|
||||
SDL_GetRendererInfo( renderer, &info );
|
||||
cout << "Renderer name: " << info.name << endl;
|
||||
cout << "Texture formats: " << endl;
|
||||
for( Uint32 i = 0; i < info.num_texture_formats; i++ )
|
||||
{
|
||||
cout << SDL_GetPixelFormatName( info.texture_formats[i] ) << endl;
|
||||
}
|
||||
|
||||
const unsigned int texWidth = 1024;
|
||||
const unsigned int texHeight = 1024;
|
||||
SDL_Texture* texture = SDL_CreateTexture
|
||||
(
|
||||
renderer,
|
||||
SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
texWidth, texHeight
|
||||
);
|
||||
|
||||
vector< unsigned char > pixels( texWidth * texHeight * 4, 0 );
|
||||
|
||||
SDL_Event event;
|
||||
bool running = true;
|
||||
while( running )
|
||||
{
|
||||
const Uint64 start = SDL_GetPerformanceCounter();
|
||||
|
||||
SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
|
||||
SDL_RenderClear( renderer );
|
||||
|
||||
while( SDL_PollEvent( &event ) )
|
||||
{
|
||||
if( ( SDL_QUIT == event.type ) ||
|
||||
( SDL_KEYDOWN == event.type && SDL_SCANCODE_ESCAPE == event.key.keysym.scancode ) )
|
||||
{
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// splat down some random pixels
|
||||
for( unsigned int i = 0; i < 1000; i++ )
|
||||
{
|
||||
const unsigned int x = rand() % texWidth;
|
||||
const unsigned int y = rand() % texHeight;
|
||||
|
||||
const unsigned int offset = ( texWidth * 4 * y ) + x * 4;
|
||||
pixels[ offset + 0 ] = rand() % 256; // b
|
||||
pixels[ offset + 1 ] = rand() % 256; // g
|
||||
pixels[ offset + 2 ] = rand() % 256; // r
|
||||
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
|
||||
}
|
||||
|
||||
//unsigned char* lockedPixels;
|
||||
//int pitch;
|
||||
//SDL_LockTexture
|
||||
// (
|
||||
// texture,
|
||||
// NULL,
|
||||
// reinterpret_cast< void** >( &lockedPixels ),
|
||||
// &pitch
|
||||
// );
|
||||
//std::copy( pixels.begin(), pixels.end(), lockedPixels );
|
||||
//SDL_UnlockTexture( texture );
|
||||
|
||||
SDL_UpdateTexture
|
||||
(
|
||||
texture,
|
||||
NULL,
|
||||
&pixels[0],
|
||||
texWidth * 4
|
||||
);
|
||||
|
||||
SDL_RenderCopy( renderer, texture, NULL, NULL );
|
||||
SDL_RenderPresent( renderer );
|
||||
|
||||
const Uint64 end = SDL_GetPerformanceCounter();
|
||||
const static Uint64 freq = SDL_GetPerformanceFrequency();
|
||||
const double seconds = ( end - start ) / static_cast< double >( freq );
|
||||
cout << "Frame time: " << seconds * 1000.0 << "ms" << endl;
|
||||
}
|
||||
|
||||
SDL_DestroyRenderer( renderer );
|
||||
SDL_DestroyWindow( window );
|
||||
SDL_Quit();
|
||||
}
|
||||
BIN
test/main.o
Normal file
BIN
test/main.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user