Added files

This commit is contained in:
Robert Vokac 2025-02-22 21:35:48 +01:00
parent 49b1d1d118
commit 4fdf62b6da
Signed by: robertvokac
GPG Key ID: FB9CE8E20AADA55F
7 changed files with 163 additions and 0 deletions

17
CMakeLists.txt Normal file

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.10)
project(DEMOGAMECNA)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(../cna CNA)
include_directories(../cna/include CNA)
add_executable(DEMOGAMECNA
src/Main.cpp
src/Game1.cpp
)
find_package(SDL3 REQUIRED)
find_package(SDL3_image REQUIRED)
target_link_libraries(DEMOGAMECNA PRIVATE CNA SDL3::SDL3)

BIN
Content/player.png Normal file

Binary file not shown.

After

(image error) Size: 635 KiB

38
demo-game-cna.cbp Normal file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="demo-game-cna" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/demo-game-cna" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/demo-game-cna" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="src/Main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

5
demo-game-cna.layout Normal file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
</CodeBlocks_layout_file>

63
src/Game1.cpp Normal file

@ -0,0 +1,63 @@
#include "Game1.h"
#include <SDL3_image/SDL_image.h>
#include <cstdlib>
Game1::Game1() {
graphicsDevice = new CNA::GraphicsDevice();
spriteBatch = new CNA::SpriteBatch(graphicsDevice);
}
Game1::~Game1() {
delete spriteBatch;
delete graphicsDevice;
}
void Game1::LoadContent() {
playerTexture = LoadTexture("Content/player.png");
playerX = 100;
playerY = 100;
}
void Game1::Update(float deltaTime) {
playerX += 100 * deltaTime; // Player's move
}
private float r = ((float)(rand()%255)) /255.0f ;
private float g = ((float)(rand()%255)) /255.0f ;
private float b = ((float)(rand()%255)) /255.0f ;
void Game1::Draw() {
spriteBatch->Begin(); // Starts rendering
float r_ = ((float)(rand()%255)) /32.0f ;
float g_ = ((float)(rand()%255)) /32.0f ;
float b_ = ((float)(rand()%255)) /32.0f ;
boolean rs = ()rand() % 100) > 50;
boolean gs = ()rand() % 100) > 50;
boolean bs = ()rand() % 100) > 50;
if(rs)r_=r_*(-1);
if(rs)g_=g_*(-1);
if(rs)b_=b_*(-1);
r=r+r_;
g=g+g_;
b=b+b_;
if(r<0.0f)r=0.0f;
if(g<0.0f)g=0.0f;
if(b<0.0f)b=0.0f;
if(r>0.0f)r=1.0f;
if(g>0.0f)g=1.0f;
if(b>0.0f)b=1.0f;
graphicsDevice->Clear(r,g,b,120.0f);
graphicsDevice->Present();
spriteBatch->Draw(playerTexture, playerX, playerY);
spriteBatch->End(); // Stops rendering
}
SDL_Texture* Game1::LoadTexture(const std::string& path) {
return IMG_LoadTexture(graphicsDevice->GetRenderer(), path.c_str());
}

31
src/Game1.h Normal file

@ -0,0 +1,31 @@
#ifndef GAME1_H
#define GAME1_H
#include "CNA/Game.h"
#include "CNA/Texture2D.h"
#include "CNA/GraphicsDevice.h"
#include "CNA/SpriteBatch.h"
#include <string>
class Game1 : public CNA::Game {
public:
Game1();
virtual ~Game1();
void LoadContent() override;
void Update(float deltaTime) override;
void Draw() override;
SDL_Texture* LoadTexture(const std::string& path);
private:
//CNA::Texture2D* playerTexture;
SDL_Texture* playerTexture;
float playerX;
float playerY;
protected:
CNA::GraphicsDevice* graphicsDevice;
CNA::SpriteBatch* spriteBatch;
};
#endif // GAME1_H

9
src/Main.cpp Normal file

@ -0,0 +1,9 @@
#include "Game1.h"
#include <iostream>
int main(int argc, char* argv[]) {
std::cout<<"Starting game" <<std::endl;
Game1 game;
game.Run(); // Launching of the game.
return 0;
}