Added files
This commit is contained in:
parent
7d6a7eec8c
commit
69bdcd3fd0
17
CMakeLists.txt
Normal file
17
CMakeLists.txt
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(CNA)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
|
find_package(SDL3 REQUIRED)
|
||||||
|
find_package(SDL3_image REQUIRED)
|
||||||
|
|
||||||
|
add_library(CNA STATIC
|
||||||
|
src/Game.cpp
|
||||||
|
src/GraphicsDevice.cpp
|
||||||
|
src/SpriteBatch.cpp
|
||||||
|
src/Texture2D.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(CNA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
target_link_libraries(CNA PRIVATE SDL3::SDL3 SDL3_image)
|
45
cna.cbp
Normal file
45
cna.cbp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="cna" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/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/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="include/Game.h" />
|
||||||
|
<Unit filename="include/GraphicsDevice.h" />
|
||||||
|
<Unit filename="include/SpriteBatch.h" />
|
||||||
|
<Unit filename="include/Texture2D.h" />
|
||||||
|
<Unit filename="src/Game.cpp" />
|
||||||
|
<Unit filename="src/GraphicsDevice.cpp" />
|
||||||
|
<Unit filename="src/SpriteBatch.cpp" />
|
||||||
|
<Unit filename="src/Texture2D.cpp" />
|
||||||
|
<Extensions />
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
25
cna.layout
Normal file
25
cna.layout
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_layout_file>
|
||||||
|
<FileVersion major="1" minor="0" />
|
||||||
|
<ActiveTarget name="Debug" />
|
||||||
|
<File name="src/SpriteBatch.cpp" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="679" topLine="0" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
<File name="src/GraphicsDevice.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="87" topLine="0" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
<File name="include/Game.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="0" topLine="0" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
<File name="include/GraphicsDevice.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="399" topLine="0" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
</CodeBlocks_layout_file>
|
31
include/CNA/Game.h
Normal file
31
include/CNA/Game.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef CNA_GAME_H
|
||||||
|
#define CNA_GAME_H
|
||||||
|
|
||||||
|
#include "GraphicsDevice.h"
|
||||||
|
#include "SpriteBatch.h"
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
class Game {
|
||||||
|
public:
|
||||||
|
Game();
|
||||||
|
virtual ~Game();
|
||||||
|
|
||||||
|
void Run();
|
||||||
|
void Exit() { isRunning = false; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void Initialize();
|
||||||
|
virtual void LoadContent();
|
||||||
|
virtual void Update(float deltaTime);
|
||||||
|
virtual void Draw();
|
||||||
|
|
||||||
|
GraphicsDevice* graphicsDevice;
|
||||||
|
SpriteBatch* spriteBatch;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool isRunning;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CNA_GAME_H
|
28
include/CNA/GraphicsDevice.h
Normal file
28
include/CNA/GraphicsDevice.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef CNA_GRAPHICSDEVICE_H
|
||||||
|
#define CNA_GRAPHICSDEVICE_H
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
class GraphicsDevice {
|
||||||
|
public:
|
||||||
|
GraphicsDevice();
|
||||||
|
~GraphicsDevice();
|
||||||
|
|
||||||
|
void Clear(float r, float g, float b, float a);
|
||||||
|
void Present();
|
||||||
|
SDL_Renderer* GetRenderer();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
private:
|
||||||
|
SDL_Window* window;
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
|
||||||
|
//friend class SpriteBatch;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CNA_GRAPHICSDEVICE_H
|
23
include/CNA/SpriteBatch.h
Normal file
23
include/CNA/SpriteBatch.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef CNA_SPRITEBATCH_H
|
||||||
|
#define CNA_SPRITEBATCH_H
|
||||||
|
|
||||||
|
#include "GraphicsDevice.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
class SpriteBatch {
|
||||||
|
public:
|
||||||
|
SpriteBatch(GraphicsDevice* graphicsDevice);
|
||||||
|
~SpriteBatch();
|
||||||
|
|
||||||
|
void Begin();
|
||||||
|
void End();
|
||||||
|
void Draw(SDL_Texture* texture, float x, float y);
|
||||||
|
|
||||||
|
private:
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CNA_SPRITEBATCH_H
|
20
include/CNA/Texture2D.h
Normal file
20
include/CNA/Texture2D.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef CNA_TEXTURE2D_H
|
||||||
|
#define CNA_TEXTURE2D_H
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
class Texture2D {
|
||||||
|
public:
|
||||||
|
Texture2D(SDL_Renderer* renderer, const char* filePath);
|
||||||
|
~Texture2D();
|
||||||
|
|
||||||
|
SDL_Texture* GetTexture() const { return texture; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
SDL_Texture* texture;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CNA_TEXTURE2D_H
|
9
main.cpp
Normal file
9
main.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
cout << "Hello world!" << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
50
src/Game.cpp
Normal file
50
src/Game.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "CNA/Game.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
Game::Game() : isRunning(true) {
|
||||||
|
}
|
||||||
|
|
||||||
|
Game::~Game() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Run() {
|
||||||
|
Initialize();
|
||||||
|
int i = 0;
|
||||||
|
while (isRunning) {
|
||||||
|
SDL_Event e;
|
||||||
|
while (SDL_PollEvent(&e)) {
|
||||||
|
if (e.type == SDL_EVENT_QUIT) {
|
||||||
|
isRunning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Update(0.016f);
|
||||||
|
Draw();
|
||||||
|
i++;
|
||||||
|
std::cout<<"next frame"<<i;
|
||||||
|
SDL_Delay(50);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Initialize() {
|
||||||
|
// Initialize game resources
|
||||||
|
LoadContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::LoadContent() {
|
||||||
|
// Loading content (e.g. textures)
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Update(float deltaTime) {
|
||||||
|
// Main game logic
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::Draw() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
40
src/GraphicsDevice.cpp
Normal file
40
src/GraphicsDevice.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "CNA/GraphicsDevice.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
GraphicsDevice::GraphicsDevice() {
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
|
std::cerr << "SDL2 could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
window = SDL_CreateWindow("CNA Game", 800, 600, SDL_WINDOW_RESIZABLE);
|
||||||
|
if (!window) {
|
||||||
|
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer = SDL_CreateRenderer(window, NULL);
|
||||||
|
if (!renderer) {
|
||||||
|
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphicsDevice::~GraphicsDevice() {
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsDevice::Clear(float r, float g, float b, float a) {
|
||||||
|
SDL_SetRenderDrawColor(renderer, static_cast<Uint8>(r * 255), static_cast<Uint8>(g * 255), static_cast<Uint8>(b * 255), static_cast<Uint8>(a * 255));
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsDevice::Present() {
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
SDL_Renderer* GraphicsDevice::GetRenderer() {
|
||||||
|
return renderer;
|
||||||
|
}
|
||||||
|
}
|
24
src/SpriteBatch.cpp
Normal file
24
src/SpriteBatch.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "CNA/SpriteBatch.h"
|
||||||
|
#include <SDL3_image/SDL_image.h>
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
SpriteBatch::SpriteBatch(GraphicsDevice* graphicsDevice) {
|
||||||
|
renderer = graphicsDevice->GetRenderer();
|
||||||
|
}
|
||||||
|
|
||||||
|
SpriteBatch::~SpriteBatch() {}
|
||||||
|
|
||||||
|
void SpriteBatch::Begin() {
|
||||||
|
// Setting before rendering, for example blend mode
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpriteBatch::End() {
|
||||||
|
// Clean up after renderring
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpriteBatch::Draw(SDL_Texture* texture, float x, float y) {
|
||||||
|
SDL_FRect dstFRect = { static_cast<float>(x), static_cast<float>(y), 100.0f, 100.0f }; // 100x100 is the size of the image
|
||||||
|
SDL_RenderTexture(renderer, texture, nullptr, &dstFRect);
|
||||||
|
}
|
||||||
|
}
|
19
src/Texture2D.cpp
Normal file
19
src/Texture2D.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include "CNA/Texture2D.h"
|
||||||
|
#include <SDL3_image/SDL_image.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace CNA {
|
||||||
|
|
||||||
|
Texture2D::Texture2D(SDL_Renderer* renderer, const char* filePath) {
|
||||||
|
texture = IMG_LoadTexture(renderer, filePath);
|
||||||
|
if (!texture) {
|
||||||
|
std::cerr << "Failed to load texture: " << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture2D::~Texture2D() {
|
||||||
|
if (texture) {
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user