Color palette test, Mouse test

This commit is contained in:
Bobby Lucero 2024-04-19 15:51:57 -04:00
parent b8fcb14d0c
commit 2427e3b672
3 changed files with 143 additions and 20 deletions

View File

@ -1,4 +1,5 @@
#include <algorithm> #include <algorithm>
#include <fstream>
#include "pocketpy.h" #include "pocketpy.h"
#include "raylib.h" #include "raylib.h"
@ -14,24 +15,54 @@ int startupScreenHeight = virtualScreenHeight * initialScale;
int screenWidth = startupScreenWidth; int screenWidth = startupScreenWidth;
int screenHeight = startupScreenHeight; 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<float>(r) / 255.0f;
float gf = static_cast<float>(g) / 255.0f;
float bf = static_cast<float>(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 ar = (float)virtualScreenWidth / (float)virtualScreenHeight;
float nar = (float)screenWidth / (float)screenHeight; float nar = (float)screenWidth / (float)screenHeight;
if(nar > ar) { if(nar > ar) {
bounds.height = screenHeight; virtualScreenBounds.height = screenHeight;
bounds.width = bounds.height * ar; virtualScreenBounds.width = virtualScreenBounds.height * ar;
origin.x = -(screenWidth / 2.0f - (bounds.width / 2.0f)); origin.x = -(screenWidth / 2.0f - (virtualScreenBounds.width / 2.0f));
origin.y = 0; origin.y = 0;
}else { }else {
bounds.width = screenWidth; virtualScreenBounds.width = screenWidth;
bounds.height = bounds.width / ar; virtualScreenBounds.height = virtualScreenBounds.width / ar;
origin.x = 0; 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() { int main() {
SetTraceLogLevel(LOG_ERROR); SetTraceLogLevel(LOG_ERROR);
@ -41,17 +72,29 @@ int main() {
SetConfigFlags(FLAG_WINDOW_RESIZABLE); SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(startupScreenWidth, startupScreenHeight, "test"); InitWindow(startupScreenWidth, startupScreenHeight, "test");
SetTargetFPS(-1); SetTargetFPS(60);
RenderTexture2D virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); RenderTexture2D virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
Rectangle sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; Rectangle sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height };
Rectangle virtualScreenBounds; calculateBounds();
Vector2 origin;
calculateBounds(virtualScreenBounds, origin);
pkpy::VM* vm = new pkpy::VM(); pkpy::VM* vm = new pkpy::VM();
Texture2D logo = LoadTexture("resources/img.png"); Texture2D logo = LoadTexture("resources/img.png");
Texture2D mouse = LoadTexture("resources/mouse.png");
// Load palette
vector<Color> 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 { try {
pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false); pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false);
@ -62,12 +105,16 @@ int main() {
} }
HideCursor();
while(!WindowShouldClose()) { while(!WindowShouldClose()) {
if (IsWindowResized() && !IsWindowFullscreen()) if (IsWindowResized() && !IsWindowFullscreen())
{ {
screenWidth = GetScreenWidth(); screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = GetScreenHeight();
calculateBounds(virtualScreenBounds, origin); calculateBounds();
} }
if (IsKeyPressed(KEY_F)) { if (IsKeyPressed(KEY_F)) {
@ -83,22 +130,34 @@ int main() {
screenHeight = GetMonitorHeight(monitor); screenHeight = GetMonitorHeight(monitor);
ToggleFullscreen(); ToggleFullscreen();
} }
calculateBounds(virtualScreenBounds, origin); calculateBounds();
}else if(IsKeyPressed(KEY_I)){ }else if(IsKeyPressed(KEY_I)){
flip = !flip; flip = !flip;
} }
BeginTextureMode(virtualScreen); BeginTextureMode(virtualScreen);
ClearBackground(BLUE); ClearBackground(palette[1]);
DrawText(("Hello World " + to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE); 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); 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); DrawLineBezier((Vector2){80, 20}, (Vector2){virtualScreenWidth - 5, virtualScreenHeight - 5}, 1, GREEN);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, GREEN); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, palette[31]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, YELLOW); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, palette[28]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, ORANGE); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, palette[16]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 9, RED); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 9, palette[15]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 7, PURPLE); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 7, palette[51]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 5, BLUE); 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(); EndTextureMode();

BIN
resources/mouse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 B

64
resources/palette2.hex Normal file
View File

@ -0,0 +1,64 @@
2e222f
3e3546
625565
966c6c
ab947a
694f62
7f708a
9babb2
c7dcd0
ffffff
6e2727
b33831
ea4f36
f57d4a
ae2334
e83b3b
fb6b1d
f79617
f9c22b
7a3045
9e4539
cd683d
e6904e
fbb954
4c3e24
676633
a2a947
d5e04b
fbff86
165a4c
239063
1ebc73
91db69
cddf6c
313638
374e4a
547e64
92a984
b2ba90
0b5e65
0b8a8f
0eaf9b
30e1b9
8ff8e2
323353
484a77
4d65b4
4d9be6
8fd3ff
45293f
6b3e75
905ea9
a884f3
eaaded
753c54
a24b6f
cf657f
ed8099
831c5d
c32454
f04f78
f68181
fca790
fdcbb0