Render grid via sdl for TTT
This commit is contained in:
parent
9d5654c034
commit
4e1b5e0dea
8
TTT/.vscode/settings.json
vendored
Normal file
8
TTT/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"xstring": "cpp",
|
||||
"xutility": "cpp",
|
||||
"map": "cpp",
|
||||
"vector": "cpp"
|
||||
}
|
||||
}
|
||||
28
TTT/.vscode/tasks.json
vendored
Normal file
28
TTT/.vscode/tasks.json
vendored
Normal file
@ -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"
|
||||
}
|
||||
221
TTT/App.cpp
Normal file
221
TTT/App.cpp
Normal file
@ -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<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
|
||||
47
TTT/App.hh
Normal file
47
TTT/App.hh
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_timer.h>
|
||||
#include <SDL2/SDL2_gfxPrimitives.h>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
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<TEXTURE,SDL_Texture*> m_Textures;
|
||||
std::vector<std::vector<std::pair<SDL_Rect, TEXTURE>>> 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
|
||||
|
||||
|
||||
|
||||
BIN
TTT/BTNPenguin.png
Normal file
BIN
TTT/BTNPenguin.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.4 KiB |
@ -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
|
||||
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
|
||||
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
|
||||
)
|
||||
target_link_libraries(Tic-Tac-Toe
|
||||
PRIVATE
|
||||
$<IF:$<TARGET_EXISTS:SDL2_image::SDL2_image>,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})
|
||||
|
||||
BIN
TTT/SYMBOL_O.png
Normal file
BIN
TTT/SYMBOL_O.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 512 B |
BIN
TTT/SYMBOL_X.png
Normal file
BIN
TTT/SYMBOL_X.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 498 B |
BIN
TTT/TTT_WALL.png
Normal file
BIN
TTT/TTT_WALL.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 618 B |
14
TTT/main.cpp
14
TTT/main.cpp
@ -1,5 +1,11 @@
|
||||
#include <iostream>
|
||||
#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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user