169 lines
4.8 KiB
C++
Executable File
169 lines
4.8 KiB
C++
Executable File
#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
|