diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2995c6e --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/cna.cbp b/cna.cbp new file mode 100644 index 0000000..a99f8a2 --- /dev/null +++ b/cna.cbp @@ -0,0 +1,45 @@ + + + + + + diff --git a/cna.layout b/cna.layout new file mode 100644 index 0000000..29d9404 --- /dev/null +++ b/cna.layout @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/include/CNA/Game.h b/include/CNA/Game.h new file mode 100644 index 0000000..5ca1782 --- /dev/null +++ b/include/CNA/Game.h @@ -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 diff --git a/include/CNA/GraphicsDevice.h b/include/CNA/GraphicsDevice.h new file mode 100644 index 0000000..107a75c --- /dev/null +++ b/include/CNA/GraphicsDevice.h @@ -0,0 +1,28 @@ +#ifndef CNA_GRAPHICSDEVICE_H +#define CNA_GRAPHICSDEVICE_H + +#include + +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 diff --git a/include/CNA/SpriteBatch.h b/include/CNA/SpriteBatch.h new file mode 100644 index 0000000..4c52508 --- /dev/null +++ b/include/CNA/SpriteBatch.h @@ -0,0 +1,23 @@ +#ifndef CNA_SPRITEBATCH_H +#define CNA_SPRITEBATCH_H + +#include "GraphicsDevice.h" +#include + +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 diff --git a/include/CNA/Texture2D.h b/include/CNA/Texture2D.h new file mode 100644 index 0000000..f3ea935 --- /dev/null +++ b/include/CNA/Texture2D.h @@ -0,0 +1,20 @@ +#ifndef CNA_TEXTURE2D_H +#define CNA_TEXTURE2D_H + +#include + +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 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b4392ec --- /dev/null +++ b/main.cpp @@ -0,0 +1,9 @@ +#include + +using namespace std; + +int main() +{ + cout << "Hello world!" << endl; + return 0; +} diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..f597b0f --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,50 @@ +#include "CNA/Game.h" +#include +#include + + +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"< +#include + +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(r * 255), static_cast(g * 255), static_cast(b * 255), static_cast(a * 255)); + SDL_RenderClear(renderer); + } + + void GraphicsDevice::Present() { + SDL_RenderPresent(renderer); + } + SDL_Renderer* GraphicsDevice::GetRenderer() { + return renderer; + } +} diff --git a/src/SpriteBatch.cpp b/src/SpriteBatch.cpp new file mode 100644 index 0000000..6c7293d --- /dev/null +++ b/src/SpriteBatch.cpp @@ -0,0 +1,24 @@ +#include "CNA/SpriteBatch.h" +#include + +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(x), static_cast(y), 100.0f, 100.0f }; // 100x100 is the size of the image + SDL_RenderTexture(renderer, texture, nullptr, &dstFRect); + } +} diff --git a/src/Texture2D.cpp b/src/Texture2D.cpp new file mode 100644 index 0000000..6802a81 --- /dev/null +++ b/src/Texture2D.cpp @@ -0,0 +1,19 @@ +#include "CNA/Texture2D.h" +#include +#include + +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); + } + } +}