diff --git a/TTT/.vscode/settings.json b/TTT/.vscode/settings.json new file mode 100644 index 0000000..1568bed --- /dev/null +++ b/TTT/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.associations": { + "xstring": "cpp", + "xutility": "cpp", + "map": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/TTT/.vscode/tasks.json b/TTT/.vscode/tasks.json new file mode 100644 index 0000000..6532339 --- /dev/null +++ b/TTT/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: cl.exe build active file", + "command": "cl.exe", + "args": [ + "/Zi", + "/EHsc", + "/nologo", + "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe", + "${file}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$msCompile" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/TTT/App.cpp b/TTT/App.cpp new file mode 100644 index 0000000..835bb31 --- /dev/null +++ b/TTT/App.cpp @@ -0,0 +1,221 @@ +#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(); + 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 \ No newline at end of file diff --git a/TTT/App.hh b/TTT/App.hh new file mode 100644 index 0000000..a315487 --- /dev/null +++ b/TTT/App.hh @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +namespace TTT +{ + +enum TEXTURE { + NONE, + WALL, + SYMBOL_X, + SYMBOL_O +}; + +class App +{ +private: + int m_WindowWidth; + int m_WindowHeight; + int m_Close; + int m_GridHeight; + int m_GridWidth; + SDL_Window* m_Window; + SDL_Renderer* m_Renderer; + SDL_Surface* m_Surface; + std::map m_Textures; + std::vector>> m_Grid; + +public: + App(int width, int height); + void DrawToGridCell(TEXTURE textureId, int x, int y); + void DrawGrid(); + void UpdateGridHover(int x, int y); + void GridRenderCopy(); + ~App(); +}; + +} // namespace TTT + + + diff --git a/TTT/BTNPenguin.png b/TTT/BTNPenguin.png new file mode 100644 index 0000000..cbd622e Binary files /dev/null and b/TTT/BTNPenguin.png differ diff --git a/TTT/CMakeLists.txt b/TTT/CMakeLists.txt index b9549d7..79a1cfa 100644 --- a/TTT/CMakeLists.txt +++ b/TTT/CMakeLists.txt @@ -2,14 +2,21 @@ cmake_minimum_required(VERSION 3.0.0) project(Tic-Tac-Toe VERSION 0.1.0) find_package(SDL2 CONFIG REQUIRED) - -add_executable(Tic-Tac-Toe main.cpp) +find_package(SDL2_image CONFIG REQUIRED) +find_package(sdl2-gfx CONFIG REQUIRED) + +add_executable(Tic-Tac-Toe main.cpp App.cpp App.hh) target_link_libraries(Tic-Tac-Toe PRIVATE $ $,SDL2::SDL2,SDL2::SDL2-static> ) +target_link_libraries(Tic-Tac-Toe + PRIVATE + $,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static> +) +target_link_libraries(Tic-Tac-Toe PRIVATE SDL2::SDL2_gfx) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) diff --git a/TTT/SYMBOL_O.png b/TTT/SYMBOL_O.png new file mode 100644 index 0000000..0506f35 Binary files /dev/null and b/TTT/SYMBOL_O.png differ diff --git a/TTT/SYMBOL_X.png b/TTT/SYMBOL_X.png new file mode 100644 index 0000000..4c9f490 Binary files /dev/null and b/TTT/SYMBOL_X.png differ diff --git a/TTT/TTT_WALL.png b/TTT/TTT_WALL.png new file mode 100644 index 0000000..05d60eb Binary files /dev/null and b/TTT/TTT_WALL.png differ diff --git a/TTT/main.cpp b/TTT/main.cpp index c4a394d..b6308f0 100644 --- a/TTT/main.cpp +++ b/TTT/main.cpp @@ -1,5 +1,11 @@ -#include +#include "App.hh" -int main(int, char**) { - std::cout << "Hello, world!\n"; -} +using namespace TTT; + +int main(int argc, char *argv[]) +{ + + App app = App(1000, 1000); + + return 0; +} \ No newline at end of file