Files
2dkg/2dgk_3/2dgk_3/main.cpp
2021-10-30 19:36:36 +02:00

465 lines
8.2 KiB
C++

//Using SDL and standard IO
#include "SDL.h"
#include <cstdio>
#include <string>
#include <fstream>
#include <vector>
//Screen dimension constants
const int SCREEN_WIDTH = 896;
const int SCREEN_HEIGHT = 1024;
const int TILE_WIDTH = 32;
const int TILE_HEIGHT = 32;
const float SPEED = 0.16f;
const float SMOOTH = 0.5f;
enum TILE_TYPE {
DEFAULT = 0,
HORIZONTAL_WALL = 1,
VERTICAL_WALL = 2,
DOT = 3
};
class Vector2D {
public:
Vector2D(float x, float y) : m_x(x), m_y(y) {}
float getX() { return m_x; }
float getY() { return m_y; }
void setX(float x) { m_x = x; }
void setY(float y) { m_y = y; }
float length() { return sqrt(m_x * m_x + m_y * m_y); }
Vector2D operator+(const Vector2D& v2) const {
return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
}
friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) {
v1.m_x += v2.m_x;
v1.m_y += v2.m_y;
return v1;
}
Vector2D operator*(float scalar) {
return Vector2D(m_x * scalar, m_y * scalar);
}
Vector2D& operator*=(float scalar) {
m_x *= scalar;
m_y *= scalar;
return *this;
}
Vector2D operator-(const Vector2D& v2) const {
return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
}
friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) {
v1.m_x -= v2.m_x;
v1.m_y -= v2.m_y;
return v1;
}
Vector2D operator/(float scalar) {
return Vector2D(m_x / scalar, m_y / scalar);
}
Vector2D& operator/=(float scalar) {
m_x /= scalar;
m_y /= scalar;
return *this;
}
void normalize() {
float l = length();
if (l > 0) {
(*this) *= 1 / l;
}
}
private:
float m_x;
float m_y;
};
class KTile {
public:
//Initializes position and type
KTile(int x, int y, TILE_TYPE tileType);
//Shows the tile
void render();
//Get the tile type
int getType();
SDL_Rect getBox();
private:
//The attributes of the tile
SDL_Rect mBox;
//The tile type
TILE_TYPE mType;
};
int KTile::getType()
{
return mType;
}
SDL_Rect KTile::getBox()
{
return mBox;
}
//Texture wrapper class
class KTexture
{
public:
//Initializes variables
KTexture();
//Deallocates memory
~KTexture();
//Loads image at specified path
bool loadFromFile(std::string path);
//Deallocates texture
void free();
//Renders texture at given point
void render(int x, int y, SDL_Rect* clip = NULL);
//Gets image dimensions
int getWidth();
int getHeight();
private:
//The actual hardware texture
SDL_Texture* mTexture;
//Image dimensions
int mWidth;
int mHeight;
};
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
KTexture gTileTextures[4];
std::vector<KTile> gTiles;
bool init();
bool loadMedia();
void close();
SDL_Texture* loadTexture(std::string path);
KTexture::KTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
KTexture::~KTexture()
{
//Deallocate
free();
}
bool KTexture::loadFromFile(std::string path)
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
//Color key image
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0));
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
//Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
//Return success
mTexture = newTexture;
return mTexture != NULL;
}
void KTexture::free()
{
//Free texture if it exists
if (mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void KTexture::render(int x, int y, SDL_Rect* clip)
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad);
}
int KTexture::getWidth()
{
return mWidth;
}
int KTexture::getHeight()
{
return mHeight;
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
//Create window
gWindow = SDL_CreateWindow("2DGK - Zadanie 3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF);
}
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
for (int i = TILE_TYPE::DEFAULT; i <= TILE_TYPE::DOT; ++i)
{
if (!gTileTextures[i].loadFromFile("textures/0" + std::to_string(i) + ".bmp"))
{
printf("Failed to load 0%d texture image!\n", i);
success = false;
}
}
gTiles.clear();
std::ifstream levelFile;
levelFile.open("level.txt");
if (levelFile.fail())
{
printf("Failed to load level.txt!\n");
success = false;
}
else
{
int y = 0;
std::string line;
while (std::getline(levelFile, line)) {
for (int i = 0; i < line.length(); ++i) {
TILE_TYPE type = TILE_TYPE::DEFAULT;
switch (line[i])
{
case '|':
type = TILE_TYPE::VERTICAL_WALL;
break;
case '-':
type = TILE_TYPE::HORIZONTAL_WALL;
break;
case '.':
type = TILE_TYPE::DOT;
break;
}
gTiles.push_back(KTile(i*TILE_WIDTH, y*TILE_HEIGHT, type));
}
++y;
}
levelFile.close();
}
return success;
}
SDL_Texture* loadTexture(std::string path)
{
//The final optimized image
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
return newTexture;
}
void close()
{
//Free loaded images
for (int i = 0; i <= TILE_TYPE::DOT; ++i)
gTileTextures[i].free();
//Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
//Load media
if (!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while (!quit)
{//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
}
//Clear screen
SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF);
SDL_RenderClear(gRenderer);
for (auto& tile : gTiles)
tile.render();
//Update screen
SDL_RenderPresent(gRenderer);
}
}
}
//Free resources and close SDL
close();
return 0;
}
KTile::KTile(int x, int y, TILE_TYPE tileType)
{
//Get the offsets
mBox.x = x;
mBox.y = y;
mBox.w = TILE_WIDTH;
mBox.h = TILE_HEIGHT;
//Get the tile type
mType = tileType;
}
void KTile::render()
{
gTileTextures[mType].render(mBox.x, mBox.y);
}