Initial commit

This commit is contained in:
Daniel Cortes
2020-05-22 01:54:52 -04:00
commit 242e60ff40
42 changed files with 1557 additions and 0 deletions

8
fez/.idea/fez.iml generated Normal file
View 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
View 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
View 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
View 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
View 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
View 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

Binary file not shown.

55
fez/paterns Normal file
View File

@@ -0,0 +1,55 @@
Letters:
0
Numbers:
Controls:

BIN
fez/run Executable file

Binary file not shown.