221 lines
6.4 KiB
C++
221 lines
6.4 KiB
C++
#include "App.hh"
|
|
|
|
namespace TTT
|
|
{
|
|
|
|
App::App(int width, int height) :
|
|
m_WindowHeight(height), m_WindowWidth(width), m_Close(0), m_GridHeight(5), m_GridWidth(5)
|
|
{
|
|
// returns zero on success else non-zero
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
|
SDL_LogCritical(0,"error initializing SDL: %s\n", SDL_GetError());
|
|
}
|
|
m_Window = SDL_CreateWindow("Tic-Tac-Toe", // creates a window
|
|
SDL_WINDOWPOS_CENTERED,
|
|
SDL_WINDOWPOS_CENTERED,
|
|
m_WindowHeight, m_WindowHeight, 0);
|
|
|
|
// Initialize grid. Look at this transposed.
|
|
auto pair = std::pair<SDL_Rect, TEXTURE>();
|
|
pair.second = WALL;
|
|
m_Grid = {
|
|
{ pair, pair, pair, pair, pair },
|
|
{ pair, pair, pair, pair, pair },
|
|
{ pair, pair, pair, pair, pair },
|
|
{ pair, pair, pair, pair, pair },
|
|
{ pair, pair, pair, pair, pair }
|
|
};
|
|
|
|
m_Grid[0][0].second = NONE;
|
|
|
|
|
|
// triggers the program that controls
|
|
// your graphics hardware and sets flags
|
|
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
|
|
|
|
// creates a renderer to render our images
|
|
m_Renderer = SDL_CreateRenderer(m_Window, -1, render_flags);
|
|
|
|
// please provide a path for your image
|
|
SDL_Surface* wall_surface = IMG_Load("./res/TTT_WALL.png");
|
|
SDL_Surface* symbol_x_surface = IMG_Load("./res/SYMBOL_X.png");
|
|
SDL_Surface* symbol_o_surface = IMG_Load("./res/SYMBOL_O.png");
|
|
|
|
// loads image to our graphics hardware memory.
|
|
m_Textures[TTT::WALL] = SDL_CreateTextureFromSurface(m_Renderer, wall_surface);
|
|
m_Textures[TTT::SYMBOL_X] = SDL_CreateTextureFromSurface(m_Renderer, symbol_x_surface);
|
|
m_Textures[TTT::SYMBOL_O] = SDL_CreateTextureFromSurface(m_Renderer, symbol_o_surface);
|
|
|
|
// clears surfaces from main-memory.
|
|
SDL_FreeSurface(wall_surface);
|
|
SDL_FreeSurface(symbol_x_surface);
|
|
SDL_FreeSurface(symbol_o_surface);
|
|
|
|
// let us control our image position
|
|
// so that we can move it with our keyboard.
|
|
SDL_Rect dest;
|
|
SDL_Rect dest2;
|
|
// connects our texture with dest to control position
|
|
SDL_QueryTexture(m_Textures[TTT::WALL], NULL, NULL, &dest.w, &dest.h);
|
|
SDL_QueryTexture(m_Textures[TTT::SYMBOL_O], NULL, NULL, &dest2.w, &dest2.h);
|
|
|
|
// adjust height and width of our image box.
|
|
// dest.w /= 6;
|
|
// dest.h /= 6;
|
|
|
|
// sets initial x-position of object
|
|
dest.x = (m_WindowWidth - dest.w) / 2;
|
|
dest2.x = (m_WindowWidth - dest2.w) / 2;
|
|
// sets initial y-position of object
|
|
dest.y = (m_WindowHeight - dest.h) / 2;
|
|
dest2.y = (m_WindowHeight - dest2.h) / 2;
|
|
|
|
DrawGrid();
|
|
|
|
// speed of box
|
|
int speed = 300;
|
|
|
|
// animation loop
|
|
while (!m_Close) {
|
|
SDL_Event event;
|
|
|
|
// Events management
|
|
while (SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
|
|
case SDL_QUIT:
|
|
// handling of close button
|
|
m_Close = 1;
|
|
break;
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
switch (event.button.button)
|
|
{
|
|
case SDL_BUTTON(SDL_BUTTON_LEFT):
|
|
SDL_LogInfo(0, "LEFT CLICK (%d, %d)\n", event.motion.x, event.motion.y);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
// keyboard API for key pressed
|
|
switch (event.key.keysym.scancode) {
|
|
case SDL_SCANCODE_ESCAPE:
|
|
m_Close = 1;
|
|
break;
|
|
case SDL_SCANCODE_W:
|
|
case SDL_SCANCODE_UP:
|
|
dest.y -= speed / 30;
|
|
break;
|
|
case SDL_SCANCODE_A:
|
|
case SDL_SCANCODE_LEFT:
|
|
dest.x -= speed / 30;
|
|
break;
|
|
case SDL_SCANCODE_S:
|
|
case SDL_SCANCODE_DOWN:
|
|
dest.y += speed / 30;
|
|
break;
|
|
case SDL_SCANCODE_D:
|
|
case SDL_SCANCODE_RIGHT:
|
|
dest.x += speed / 30;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// right boundary
|
|
if (dest.x + dest.w > 1000)
|
|
dest.x = 1000 - dest.w;
|
|
|
|
// left boundary
|
|
if (dest.x < 0)
|
|
dest.x = 0;
|
|
|
|
// bottom boundary
|
|
if (dest.y + dest.h > 1000)
|
|
dest.y = 1000 - dest.h;
|
|
|
|
// upper boundary
|
|
if (dest.y < 0)
|
|
dest.y = 0;
|
|
|
|
// clears the screen
|
|
SDL_RenderClear(m_Renderer);
|
|
SDL_RenderCopy(m_Renderer, m_Textures[TTT::WALL], NULL, &dest);
|
|
SDL_RenderCopy(m_Renderer, m_Textures[TTT::SYMBOL_O], NULL, &dest2);
|
|
GridRenderCopy();
|
|
|
|
// triggers the double buffers
|
|
// for multiple rendering
|
|
SDL_RenderPresent(m_Renderer);
|
|
|
|
// calculates to 60 fps
|
|
SDL_Delay(1000 / 60);
|
|
}
|
|
|
|
// destroy texture
|
|
SDL_DestroyTexture(m_Textures[TTT::SYMBOL_O]);
|
|
}
|
|
|
|
void App::DrawToGridCell(TEXTURE textureId, int x, int y)
|
|
{
|
|
|
|
}
|
|
|
|
void App::DrawGrid()
|
|
{
|
|
int CELL_WIDTH = 64;
|
|
int CELL_HEIGHT = 64;
|
|
|
|
|
|
for (int i = 0; i <= m_GridWidth - 1; ++i)
|
|
{
|
|
for (int j = 0; j <= m_GridHeight - 1; ++j)
|
|
{
|
|
// connects our texture with dest to control position
|
|
SDL_QueryTexture(
|
|
m_Textures[m_Grid[i][j].second], NULL, NULL,
|
|
&m_Grid[i][j].first.w, &m_Grid[i][j].first.h);
|
|
|
|
// m_Grid[i][j].first.x = (m_WindowWidth - m_Grid[i][j].first.w) / 2;
|
|
// m_Grid[i][j].first.y = (m_WindowHeight - m_Grid[i][j].first.h) / 2;
|
|
|
|
m_Grid[i][j].first.w = CELL_WIDTH;
|
|
m_Grid[i][j].first.h = CELL_HEIGHT;
|
|
m_Grid[i][j].first.x = m_Grid[i][j].first.w * i;
|
|
m_Grid[i][j].first.y = m_Grid[i][j].first.h * j;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void App::UpdateGridHover(int x, int y)
|
|
{
|
|
}
|
|
|
|
void App::GridRenderCopy()
|
|
{
|
|
for (int i = 0; i <= m_GridWidth - 1; ++i)
|
|
{
|
|
for (int j = 0; j <= m_GridHeight - 1; ++j)
|
|
{
|
|
SDL_RenderCopy(m_Renderer, m_Textures[m_Grid[i][j].second], NULL, &m_Grid[i][j].first);
|
|
}
|
|
}
|
|
}
|
|
|
|
App::~App()
|
|
{
|
|
|
|
// destroy renderer
|
|
SDL_DestroyRenderer(m_Renderer);
|
|
// destroy window
|
|
SDL_DestroyWindow(m_Window);
|
|
// close SDL
|
|
SDL_Quit();
|
|
}
|
|
|
|
|
|
} // namespace TTT
|