From 2427e3b672a752da845040f09af3f5541433cc60 Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Fri, 19 Apr 2024 15:51:57 -0400 Subject: [PATCH] Color palette test, Mouse test --- main.cpp | 99 ++++++++++++++++++++++++++++++++--------- resources/mouse.png | Bin 0 -> 194 bytes resources/palette2.hex | 64 ++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 20 deletions(-) create mode 100644 resources/mouse.png create mode 100644 resources/palette2.hex diff --git a/main.cpp b/main.cpp index 3b5cb69..facab74 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include "pocketpy.h" #include "raylib.h" @@ -14,24 +15,54 @@ int startupScreenHeight = virtualScreenHeight * initialScale; int screenWidth = startupScreenWidth; int screenHeight = startupScreenHeight; -void calculateBounds(Rectangle& bounds, Vector2& origin) { +Rectangle virtualScreenBounds; +Vector2 origin; + +Color ColorFromHex(int hexValue) { + // Extract red, green, blue, and alpha components from the hexadecimal value + int r = (hexValue >> 16) & 0xFF; + int g = (hexValue >> 8) & 0xFF; + int b = hexValue & 0xFF; + + // Normalize the color components to the range [0, 1] + float rf = static_cast(r) / 255.0f; + float gf = static_cast(g) / 255.0f; + float bf = static_cast(b) / 255.0f; + + // Create and return the color + return ColorFromNormalized({ rf, gf, bf, 1.0f }); // Alpha is set to 1.0 (fully opaque) +} + +void calculateBounds() { float ar = (float)virtualScreenWidth / (float)virtualScreenHeight; float nar = (float)screenWidth / (float)screenHeight; if(nar > ar) { - bounds.height = screenHeight; - bounds.width = bounds.height * ar; - origin.x = -(screenWidth / 2.0f - (bounds.width / 2.0f)); + virtualScreenBounds.height = screenHeight; + virtualScreenBounds.width = virtualScreenBounds.height * ar; + origin.x = -(screenWidth / 2.0f - (virtualScreenBounds.width / 2.0f)); origin.y = 0; }else { - bounds.width = screenWidth; - bounds.height = bounds.width / ar; + virtualScreenBounds.width = screenWidth; + virtualScreenBounds.height = virtualScreenBounds.width / ar; origin.x = 0; - origin.y = -(screenHeight / 2.0f - (bounds.height / 2.0f)); + origin.y = -(screenHeight / 2.0f - (virtualScreenBounds.height / 2.0f)); } } +int mouseX(){ + int x = GetMouseX(); + float adjX = x + origin.x; + return adjX / virtualScreenBounds.width * virtualScreenWidth; +} + +int mouseY(){ + int y = GetMouseY(); + float adjY = y + origin.y; + return adjY / virtualScreenBounds.height * virtualScreenHeight; +} + int main() { SetTraceLogLevel(LOG_ERROR); @@ -41,17 +72,29 @@ int main() { SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(startupScreenWidth, startupScreenHeight, "test"); - SetTargetFPS(-1); + SetTargetFPS(60); RenderTexture2D virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); Rectangle sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; - Rectangle virtualScreenBounds; - Vector2 origin; - calculateBounds(virtualScreenBounds, origin); + calculateBounds(); pkpy::VM* vm = new pkpy::VM(); Texture2D logo = LoadTexture("resources/img.png"); + Texture2D mouse = LoadTexture("resources/mouse.png"); + + // Load palette + vector palette; + + ifstream paletteFile("resources/palette2.hex"); + string line; + + if(paletteFile.is_open()){ + while(getline(paletteFile, line)){ + palette.push_back(ColorFromHex(stoi(line, nullptr, 16))); + } + paletteFile.close(); + } try { pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false); @@ -62,12 +105,16 @@ int main() { } + HideCursor(); + while(!WindowShouldClose()) { + + if (IsWindowResized() && !IsWindowFullscreen()) { screenWidth = GetScreenWidth(); screenHeight = GetScreenHeight(); - calculateBounds(virtualScreenBounds, origin); + calculateBounds(); } if (IsKeyPressed(KEY_F)) { @@ -83,22 +130,34 @@ int main() { screenHeight = GetMonitorHeight(monitor); ToggleFullscreen(); } - calculateBounds(virtualScreenBounds, origin); + calculateBounds(); }else if(IsKeyPressed(KEY_I)){ flip = !flip; } BeginTextureMode(virtualScreen); - ClearBackground(BLUE); + ClearBackground(palette[1]); DrawText(("Hello World " + to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE); DrawTexture(logo, 80 + cos(GetTime()) * (virtualScreenHeight / 4), (virtualScreenHeight / 2) + sin(GetTime()) * (virtualScreenHeight / 4) - 29, WHITE); DrawLineBezier((Vector2){80, 20}, (Vector2){virtualScreenWidth - 5, virtualScreenHeight - 5}, 1, GREEN); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, GREEN); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, YELLOW); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, ORANGE); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 9, RED); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 7, PURPLE); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 5, BLUE); + DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, palette[31]); + DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, palette[28]); + DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, palette[16]); + DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 9, palette[15]); + DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 7, palette[51]); + DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 5, palette[47]); + + DrawRectangle(3, 19, 33, 33, BLACK); + + for (int i = 0; i < 8; ++i) { + for (int j = 0; j < 8; ++j) { + DrawRectangle(4 + i * 4, 20 + j * 4, 3, 3, palette[i + j * 8]); + } + } + + DrawTexture(mouse, mouseX(), mouseY(), WHITE); + DrawText(to_string(mouseX()).c_str(), 2, 70, 5, palette[37]); + DrawText(to_string(mouseY()).c_str(), 2, 78, 5, palette[27]); EndTextureMode(); diff --git a/resources/mouse.png b/resources/mouse.png new file mode 100644 index 0000000000000000000000000000000000000000..7a5a7bec00a0213a0f315caf71e8d82fa8867ac8 GIT binary patch literal 194 zcmeAS@N?(olHy`uVBq!ia0vp^>_E)L!3HEN&baUbDaPU;cPEB*=VV?2IW?Xxjv*3L zlLLYbey(4V@X6jt=f!^@kep!HJgMmF6^-QWF<6_`_9gV~CW zE!ouAbPn7--ShCtN_QTe7ec4HmKB%@Gcz+ghn1Pd2&A#zxiR;)pWy+Kg9ko^zLt16 m=fs8u|4%R;I&k0s14GQF