Update Gitignore and Basic Camera / Level Implementation

This commit is contained in:
2024-03-08 12:27:04 +01:00
parent 3dcfc744d5
commit 9dbfef5e78
15 changed files with 209 additions and 63 deletions

4
.gitignore vendored
View File

@@ -396,3 +396,7 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
# Game Project specifics -> do not ignore:
!**/Libraries/**/x86/
!**/Libraries/**/x64/

View File

@@ -11,3 +11,5 @@
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

View File

@@ -4,94 +4,87 @@
//-----------------------------------------------------------------
// Window Constructors
//-----------------------------------------------------------------
Window::Window( const std::string& title , float width , float height , bool isVSyncOn )
:title{ title }
,width{ width }
,height{ height }
,isVSyncOn{ isVSyncOn }
{
Window::Window(const std::string& title, float width, float height, bool isVSyncOn)
: title { title }
, width { width }
, height { height }
, isVSyncOn { isVSyncOn } {
}
//-----------------------------------------------------------------
// Point2f Constructors
//-----------------------------------------------------------------
Point2f::Point2f( )
:Point2f{ 0.0f, 0.0f }
{
Point2f::Point2f()
: Point2f { 0.0f, 0.0f } {
}
Point2f::Point2f( float x, float y )
:x{ x }, y{ y }
{
Point2f::Point2f(float x, float y)
: x { x }, y { y } {
}
// Point2f::Point2f(int x, int y)
// : x { (float)x }, y { (float)y } {
// }
//-----------------------------------------------------------------
// Rectf Constructors
//-----------------------------------------------------------------
Rectf::Rectf( )
:Rectf{ 0.0f, 0.0f, 0.0f, 0.0f }
{
Rectf::Rectf()
: Rectf { 0.0f, 0.0f, 0.0f, 0.0f } {
}
Rectf::Rectf( float left, float bottom, float width, float height )
:left{ left }
,bottom{ bottom }
,width{ width }
,height{ height }
{
Rectf::Rectf(float left, float bottom, float width, float height)
: left { left }
, bottom { bottom }
, width { width }
, height { height } {
}
// Rectf::Rectf(int left, int bottom, int width, int height) : left { (float)left }, bottom { (float)bottom }, width { (float)width }, height { (float)height } {
// }
//-----------------------------------------------------------------
// Color4f Constructors
//-----------------------------------------------------------------
Color4f::Color4f( )
:Color4f{ 0.0f, 0.0f, 0.0f, 1.0f }
{
Color4f::Color4f()
: Color4f { 0.0f, 0.0f, 0.0f, 1.0f } {
}
Color4f::Color4f( float r, float g, float b, float a )
:r{ r }
,g{ g }
,b{ b }
,a{ a }
{
Color4f::Color4f(float r, float g, float b, float a)
: r { r }
, g { g }
, b { b }
, a { a } {
}
//-----------------------------------------------------------------
// Circlef Constructors
//-----------------------------------------------------------------
Circlef::Circlef( )
:Circlef{ 0.0f, 0.0f, 0.0f }
{
Circlef::Circlef()
: Circlef { 0.0f, 0.0f, 0.0f } {
}
Circlef::Circlef( float centerX, float centerY, float radius )
:Circlef{ Point2f{ centerX, centerY }, radius }
{
Circlef::Circlef(float centerX, float centerY, float radius)
: Circlef { Point2f { centerX, centerY }, radius } {
}
Circlef::Circlef( const Point2f& center, float radius )
:center{ center }
,radius{ radius }
{
Circlef::Circlef(const Point2f& center, float radius)
: center { center }
, radius { radius } {
}
//-----------------------------------------------------------------
// Ellipsef Constructors
//-----------------------------------------------------------------
Ellipsef::Ellipsef( )
:Ellipsef{ 0.0f, 0.0f, 0.0f, 0.0f }
{
Ellipsef::Ellipsef()
: Ellipsef { 0.0f, 0.0f, 0.0f, 0.0f } {
}
Ellipsef::Ellipsef( const Point2f& center, float radiusX, float radiusY )
: center{ center }
, radiusX{ radiusX }
, radiusY{ radiusY }
{
Ellipsef::Ellipsef(const Point2f& center, float radiusX, float radiusY)
: center { center }
, radiusX { radiusX }
, radiusY { radiusY } {
}
Ellipsef::Ellipsef( float centerX, float centerY, float radiusX, float radiusY )
: Ellipsef{ Point2f{ centerX, centerY }, radiusX, radiusY }
{
Ellipsef::Ellipsef(float centerX, float centerY, float radiusX, float radiusY)
: Ellipsef { Point2f { centerX, centerY }, radiusX, radiusY } {
}

View File

@@ -16,6 +16,7 @@ struct Point2f
{
Point2f( );
explicit Point2f( float x, float y );
//Point2f(int x, int y); //Stupid fix for it giving an error
float x;
float y;
@@ -27,6 +28,7 @@ struct Rectf
{
Rectf( );
explicit Rectf( float left, float bottom, float width, float height );
//explicit Rectf( int left, int bottom, int width, int height ); //Stupid fix for it giving an error (same as Point2f)
float left;
float bottom;

2
Game/Camera.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include "pch.h"
#include "Camera.h"

10
Game/Camera.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
class Camera
{
public:
private:
};

View File

@@ -1,8 +1,11 @@
#include "pch.h"
#include "Game.h"
#include "utils.h"
Game::Game(const Window& window)
: BaseGame { window } {
: BaseGame { window }
{
Initialize();
}
@@ -11,27 +14,34 @@ Game::~Game() {
}
void Game::Initialize() {
m_WorldLevel = WorldLevel();
}
void Game::Cleanup() {
}
void Game::Update(float elapsedSec) {
// Check keyboard state
//const Uint8 *pStates = SDL_GetKeyboardState( nullptr );
//if ( pStates[SDL_SCANCODE_RIGHT] )
//{
// std::cout << "Right arrow key is down\n";
//}
//if ( pStates[SDL_SCANCODE_LEFT] && pStates[SDL_SCANCODE_UP])
//{
// std::cout << "Left and up arrow keys are down\n";
//}
const Uint8 *pStates = SDL_GetKeyboardState( nullptr );
if ( pStates[SDL_SCANCODE_RIGHT] ) {
m_CameraOffset.x += 100 * elapsedSec;
}
if ( pStates[SDL_SCANCODE_LEFT] ) {
m_CameraOffset.x -= 100 * elapsedSec;
}
}
void Game::Draw() const {
ClearBackground();
glPushMatrix();
{
glTranslatef(m_CameraOffset.x , m_CameraOffset.y, 0);
m_WorldLevel.Draw();
utils::SetColor(Color4f{1.0f, 0.0f, 0.0f, 1.0f});
utils::FillEllipse(0,0,20,20);
}
glPopMatrix();
}
void Game::ProcessKeyDownEvent(const SDL_KeyboardEvent& e) {

View File

@@ -1,5 +1,9 @@
#pragma once
#include <vector>
#include "BaseGame.h"
#include "WorldLevel.h"
#include "WorldTile.h"
class Game : public BaseGame
{
@@ -27,4 +31,8 @@ private:
void Initialize();
void Cleanup();
void ClearBackground() const;
Point2f m_CameraOffset{0, 0};
WorldLevel m_WorldLevel;
};

View File

@@ -147,13 +147,21 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Camera.cpp" />
<ClCompile Include="Game.cpp" />
<ClCompile Include="Level.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp" />
<ClCompile Include="WorldLevel.cpp" />
<ClCompile Include="WorldTile.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Camera.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="Level.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="WorldLevel.h" />
<ClInclude Include="WorldTile.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

10
Game/Level.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "pch.h"
#include "Level.h"
Level::Level() {
}
Level::~Level() {
}
void Level::Update(float elapsedSec) {
}
void Level::Draw() const {
}

16
Game/Level.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
class Level
{
public:
Level();
~Level();
virtual void Update(float elapsedSec);
virtual void Draw() const;
private:
};

25
Game/WorldLevel.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "pch.h"
#include "WorldLevel.h"
#include "utils.h"
WorldLevel::WorldLevel() {
for (int i{ 0 }; i < 10; ++i) {
for (int j{ 0 }; j < 10; ++j) {
m_WorldTiles[i][j] = WorldTile{ Point2f{ (float)i * 50, (float)j * 50 }, GroundTileTypes::Dirt };
}
}
}
WorldLevel::~WorldLevel() {
}
void WorldLevel::Update(float elapsedSec) {
}
void WorldLevel::Draw() const {
for (int i{ 0 }; i < 10; ++i) {
for (int j{ 0 }; j < 10; ++j) {
utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f });
utils::FillRect(m_WorldTiles[i][j].GetPosition().x, m_WorldTiles[i][j].GetPosition().y, 50, 50);
}
}
}

17
Game/WorldLevel.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include "Level.h"
#include "WorldTile.h"
class WorldLevel : public Level {
public:
WorldLevel();
~WorldLevel();
void Update(float elapsedSec) override;
void Draw() const override;
private:
WorldTile m_WorldTiles[10][10];
};

14
Game/WorldTile.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "pch.h"
#include "WorldTile.h"
#include "utils.h"
WorldTile::WorldTile() {
}
WorldTile::WorldTile(const Point2f& position, GroundTileTypes groundTileType) : m_Position { position }, m_GroundTileType { groundTileType } {
}
void WorldTile::Draw() const {
if (m_GroundTileType != GroundTileTypes::Air) {
utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f});
utils::FillRect(m_Position.x, m_Position.y, 50, 50);
}
}

25
Game/WorldTile.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
enum class GroundTileTypes
{
Air,
Dirt,
};
class WorldTile {
public:
WorldTile();
WorldTile(const Point2f& position, GroundTileTypes groundTileType);
void Draw() const;
Point2f GetPosition() const { return m_Position; }
void SetPosition(const Point2f& position) { m_Position = position; }
private:
Point2f m_Position;
GroundTileTypes m_GroundTileType;
};