commit a4f341596d270837cc977286f06331bb2197d201 Author: The-Tysonator Date: Thu Aug 10 20:55:01 2023 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1601f60 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +notes \ No newline at end of file diff --git a/WindowManager/WindowManager.cpp b/WindowManager/WindowManager.cpp new file mode 100644 index 0000000..781ea9d --- /dev/null +++ b/WindowManager/WindowManager.cpp @@ -0,0 +1,66 @@ + +#include +#include "WindowManager.h" + + + +WindowManager::WindowManager( char* title, float screen_width, float screen_height ) { + _title = title; + _screenWidth = screen_width; + _screenHeight = screen_height; + _aspectRatio = (double)screen_width / screen_height; + InitWindow(_screenWidth, _screenHeight, _title); + Screen = LoadRenderTexture(_screenWidth, _screenHeight); + ScaledScreen = LoadRenderTexture(_screenWidth, _screenHeight); + _scaledHeight = screen_height; + _scaledWidth = screen_width; +} + + +void WindowManager::UpdateScale() { + // Changed + bool hasChanged = (_screenWidth != GetScreenWidth()) || (_screenHeight != GetScreenHeight()); + + if ( hasChanged ) { + + // Update Scaled Size + int width = GetScreenWidth(); + int height = GetScreenHeight(); + double aspectRatio = (double)width / height; + + int newWidth = 0; + int newHeight = 0; + + if (aspectRatio > _aspectRatio) { + _scaledWidth = height * _aspectRatio; + _scaledHeight = height; + } else { + _scaledWidth = width; + _scaledHeight = width / _aspectRatio; + } + + // Update Texture Size + ScaledScreen = LoadRenderTexture(_scaledWidth, _scaledHeight); + + } +} + + + +void WindowManager::Display () { + + BeginTextureMode(ScaledScreen); + DrawTexturePro(Screen.texture, { 0, 0, static_cast(_screenWidth), -static_cast(_screenHeight) }, { 0, 0, static_cast(ScaledScreen.texture.width), static_cast(ScaledScreen.texture.height) }, { 0, 0 }, 0.0f, WHITE); + EndTextureMode(); + + float xStart = -(GetScreenWidth() - _scaledWidth) / 2; + float yStart = -(GetScreenHeight() - _scaledHeight) / 2; + + BeginDrawing(); + ClearBackground(BLACK); + + + DrawTexturePro(Screen.texture, { 0, 0, static_cast(_screenWidth), -static_cast(_screenHeight) }, { 0, 0, static_cast(ScaledScreen.texture.width), static_cast(ScaledScreen.texture.height) }, { xStart, yStart }, 0.0f, WHITE); + + EndDrawing(); +} \ No newline at end of file diff --git a/WindowManager/WindowManager.h b/WindowManager/WindowManager.h new file mode 100644 index 0000000..a328252 --- /dev/null +++ b/WindowManager/WindowManager.h @@ -0,0 +1,30 @@ +#ifndef WINDOWMANAGER_H +#define WINDOWMANAGER_H + +#include + + +class WindowManager +{ + public: + WindowManager(char* title, float screen_width, float screen_height); + void UpdateScale (); + void Display (); + RenderTexture2D Screen; + RenderTexture2D ScaledScreen; + + private: + + char* _title; + float _screenWidth; + float _screenHeight; + double _aspectRatio; + + int _scaledWidth; + int _scaledHeight; + + + +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ac360f1 --- /dev/null +++ b/main.cpp @@ -0,0 +1,108 @@ +#include +#include + +#include +#include +#include + +#include "./world/Chunk.h" +#include "./WindowManager/WindowManager.h" + + + + +int main() +{ + + + // Initialize the window and raylib + WindowManager windowManager("My Title", 320, 240); + + std::string userInput; + + SetTargetFPS(60); + + Texture2D texture = LoadTextureFromImage(ImageFromImage(LoadImage("tiles.png"), { 0, 0, 8, 8 })); + std::map myMap; + myMap["grass"] = &texture; + Chunk chunk(100, 100, 50, BLUE); + chunk.LoadTextures(myMap); + + // Enable window resizing + SetWindowState(FLAG_WINDOW_RESIZABLE); + + while (!WindowShouldClose()) + { + + windowManager.UpdateScale(); + + BeginTextureMode(windowManager.Screen); + ClearBackground(BLACK); + DrawText("Drawing on Render Texture", 10, 10, 20, RED); + DrawRectangle(160, 120, 160, 120, RED); + + ClearBackground(GRAY); + + double xStart = (GetScreenWidth() - windowManager.ScaledScreen.texture.width) / 2; + double yStart = (GetScreenHeight() - windowManager.ScaledScreen.texture.height) / 2; + + // Display the user input on the screen + DrawText(userInput.c_str(), 10, 10, 20, BLACK); + DrawRectangle(200, 200 / 2 - 50, 100, 100, RED); + chunk.Draw(); + + EndTextureMode(); + + // Scaled Texture + + + + float deltaTime = GetFrameTime(); + // Update + + + // Check for mouse input + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + { + int mouseX = GetMouseX(); + int mouseY = GetMouseY(); + + // Print the mouse coordinates + printf("Mouse clicked at (%d, %d)\n", mouseX, mouseY); + } + + + // Check for user input + if (IsKeyPressed(KEY_BACKSPACE) && userInput.length() > 0) + { + // Remove the last character from the input string + userInput.pop_back(); + } + else + { + // Check if any key is being pressed + int key = GetKeyPressed(); + + + if ( IsKeyPressed(KEY_A) ) { + } + + + if (key > 0) + { + // Append the pressed key to the input string + userInput += static_cast(key); + } + } + + // Display + windowManager.Display(); + + + } + + // Close the window and raylib + CloseWindow(); + + return 0; +} diff --git a/tiles.png b/tiles.png new file mode 100644 index 0000000..dd5d66c Binary files /dev/null and b/tiles.png differ diff --git a/world/Chunk.cpp b/world/Chunk.cpp new file mode 100644 index 0000000..8d92716 --- /dev/null +++ b/world/Chunk.cpp @@ -0,0 +1,29 @@ +#include +#include "Chunk.h" +#include +#include +#include +#include +#include + + + + +Chunk::Chunk(int x, int y, int size, Color color) + : m_x(x), m_y(y), m_size(size), m_color(color) +{} + + +void Chunk::LoadTextures(std::map textures) { + // Access and process the textures + myMap = textures; +} + + + +void Chunk::Draw() +{ + if (myMap["grass"]) DrawTexturePro(*(myMap["grass"]), { 0, 0, static_cast(myMap["grass"]->width), static_cast(myMap["grass"]->height) }, + { 0, 0, static_cast(myMap["grass"]->width * 2), static_cast(myMap["grass"]->height * 2) }, + { 0, 0 }, 0.0f, WHITE); +} diff --git a/world/Chunk.h b/world/Chunk.h new file mode 100644 index 0000000..3d0e68f --- /dev/null +++ b/world/Chunk.h @@ -0,0 +1,27 @@ +#ifndef SQUARE_H +#define SQUARE_H + +#include +#include +#include +#include + + +class Chunk +{ + public: + Chunk(int x, int y, int size, Color color); + void Draw(); + void LoadTextures(std::map textures); + + private: + int chunkTiles[8][8]; + int m_x; + int m_y; + int m_size; + Color m_color; + std::map myMap; + +}; + +#endif