Initial commit
This commit is contained in:
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.
Reference in New Issue
Block a user