first commit

main
Tylan Tyson 1 year ago
commit a4f341596d

2
.gitignore vendored

@ -0,0 +1,2 @@
.vscode
notes

@ -0,0 +1,66 @@
#include <raylib.h>
#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<float>(_screenWidth), -static_cast<float>(_screenHeight) }, { 0, 0, static_cast<float>(ScaledScreen.texture.width), static_cast<float>(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<float>(_screenWidth), -static_cast<float>(_screenHeight) }, { 0, 0, static_cast<float>(ScaledScreen.texture.width), static_cast<float>(ScaledScreen.texture.height) }, { xStart, yStart }, 0.0f, WHITE);
EndDrawing();
}

@ -0,0 +1,30 @@
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include <raylib.h>
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

@ -0,0 +1,108 @@
#include <raylib.h>
#include <string>
#include <map>
#include <string>
#include <memory>
#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<std::string, Texture2D*> 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<char>(key);
}
}
// Display
windowManager.Display();
}
// Close the window and raylib
CloseWindow();
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

@ -0,0 +1,29 @@
#include <raylib.h>
#include "Chunk.h"
#include <map>
#include <string>
#include <memory>
#include <iostream>
#include <raylib.h>
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<std::string, Texture2D*> textures) {
// Access and process the textures
myMap = textures;
}
void Chunk::Draw()
{
if (myMap["grass"]) DrawTexturePro(*(myMap["grass"]), { 0, 0, static_cast<float>(myMap["grass"]->width), static_cast<float>(myMap["grass"]->height) },
{ 0, 0, static_cast<float>(myMap["grass"]->width * 2), static_cast<float>(myMap["grass"]->height * 2) },
{ 0, 0 }, 0.0f, WHITE);
}

@ -0,0 +1,27 @@
#ifndef SQUARE_H
#define SQUARE_H
#include <raylib.h>
#include <map>
#include <string>
#include <memory>
class Chunk
{
public:
Chunk(int x, int y, int size, Color color);
void Draw();
void LoadTextures(std::map<std::string, Texture2D*> textures);
private:
int chunkTiles[8][8];
int m_x;
int m_y;
int m_size;
Color m_color;
std::map<std::string, Texture2D*> myMap;
};
#endif
Loading…
Cancel
Save