Initial refactoring

This is a mess, do not check out
This commit is contained in:
Bobby Lucero 2024-04-20 21:14:52 -04:00
parent d5271eea09
commit 4bdb4fca29
5 changed files with 134 additions and 113 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
cmake-build-debug cmake-build-debug
.vscode .vscode
cmake-build-release cmake-build-release
build

View File

@ -23,7 +23,9 @@ file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/s
set(PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/sources/") # Define PROJECT_INCLUDE to be the path to the include directory of the project set(PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/sources/") # Define PROJECT_INCLUDE to be the path to the include directory of the project
add_executable(UnnamedMicroPythonConsole main.cpp) add_executable(UnnamedMicroPythonConsole src/main.cpp
src/Pycron.cpp
src/Pycron.h)
add_subdirectory(dependencies/pocketpy) add_subdirectory(dependencies/pocketpy)
# Declaring our executable # Declaring our executable

View File

@ -1,24 +1,55 @@
#include <algorithm> //
#include <fstream> // Created by Bobby Lucero on 4/20/24.
#include "pocketpy.h" //
#include "raylib.h" #include "Pycron.h"
using namespace std; Pycron::Pycron() {
SetTraceLogLevel(LOG_ERROR);
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(startupScreenWidth, startupScreenHeight, "test");
SetTargetFPS(60);
virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height };
calculateBounds();
const int virtualScreenWidth = 360; vm = new pkpy::VM();
const int virtualScreenHeight = 203;
const int initialScale = 3;
int startupScreenWidth = virtualScreenWidth * initialScale; // Texture2D logo = LoadTexture("../resources/img.png");
int startupScreenHeight = virtualScreenHeight * initialScale; mouse = LoadTexture("../resources/mouse.png");
//
//
loadPalette((std::string &) "../resources/palette2.hex");
int screenWidth = startupScreenWidth; try {
int screenHeight = startupScreenHeight; pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false);
pkpy::PyObject* obj = vm->_exec(code, vm->_main);
Rectangle virtualScreenBounds; auto& str = pkpy::py_cast<pkpy::Str&>(vm, obj);
Vector2 origin; }catch (pkpy::Exception e) {
Color ColorFromHex(int hexValue) { }
HideCursor();
}
Pycron::~Pycron(){
CloseWindow();
delete vm;
}
int Pycron::mouseX(){
int x = GetMouseX();
float adjX = x + origin.x;
return adjX / virtualScreenBounds.width * virtualScreenWidth;
}
int Pycron::mouseY(){
int y = GetMouseY();
float adjY = y + origin.y;
return adjY / virtualScreenBounds.height * virtualScreenHeight;
}
Color Pycron::ColorFromHex(int hexValue) {
// Extract red, green, blue, and alpha components from the hexadecimal value // Extract red, green, blue, and alpha components from the hexadecimal value
int r = (hexValue >> 16) & 0xFF; int r = (hexValue >> 16) & 0xFF;
int g = (hexValue >> 8) & 0xFF; int g = (hexValue >> 8) & 0xFF;
@ -33,7 +64,21 @@ Color ColorFromHex(int hexValue) {
return ColorFromNormalized({ rf, gf, bf, 1.0f }); // Alpha is set to 1.0 (fully opaque) return ColorFromNormalized({ rf, gf, bf, 1.0f }); // Alpha is set to 1.0 (fully opaque)
} }
void calculateBounds() { void Pycron::loadPalette(std::string& path){
// Load palette
std::ifstream paletteFile(path);
std::string line;
if(paletteFile.is_open()){
while(getline(paletteFile, line)){
palette.push_back(ColorFromHex(stoi(line, nullptr, 16)));
}
paletteFile.close();
}
}
void Pycron::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;
@ -51,79 +96,23 @@ void calculateBounds() {
} }
} }
int mouseX(){ void Pycron::StartGameLoop() {
int x = GetMouseX(); while (!shouldClose) {
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);
bool flip = false;
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(startupScreenWidth, startupScreenHeight, "test");
SetTargetFPS(60);
RenderTexture2D virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
Rectangle sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height };
calculateBounds();
pkpy::VM* vm = new pkpy::VM();
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 {
pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false);
pkpy::PyObject* obj = vm->_exec(code, vm->_main);
pkpy::Str& str = pkpy::py_cast<pkpy::Str&>(vm, obj);
}catch (pkpy::Exception e) {
}
HideCursor();
while(!WindowShouldClose()) {
if (IsWindowResized() && !IsWindowFullscreen()) if (IsWindowResized() && !IsWindowFullscreen()) {
{
screenWidth = GetScreenWidth(); screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = GetScreenHeight();
calculateBounds(); calculateBounds();
} }
if (IsKeyPressed(KEY_F)) { if (IsKeyPressed(KEY_F)) {
if(IsWindowFullscreen()){ if (IsWindowFullscreen()) {
ToggleFullscreen(); ToggleFullscreen();
SetWindowSize(startupScreenWidth, startupScreenHeight); SetWindowSize(startupScreenWidth, startupScreenHeight);
screenWidth = startupScreenWidth; screenWidth = startupScreenWidth;
screenHeight = startupScreenHeight; screenHeight = startupScreenHeight;
}else{ } else {
int monitor = GetCurrentMonitor(); int monitor = GetCurrentMonitor();
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor)); SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
screenWidth = GetMonitorWidth(monitor); screenWidth = GetMonitorWidth(monitor);
@ -131,15 +120,16 @@ int main() {
ToggleFullscreen(); ToggleFullscreen();
} }
calculateBounds(); calculateBounds();
}else if(IsKeyPressed(KEY_I)){ }else if(IsKeyPressed(KEY_ESCAPE)){
flip = !flip; shouldClose = true;
} }
BeginTextureMode(virtualScreen); BeginTextureMode(virtualScreen);
ClearBackground(palette[1]); ClearBackground(palette[1]);
DrawText(("Hello World " + to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE); DrawText(("Hello World " + std::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.0f, virtualScreenHeight - 5.0f}, 1, GREEN);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, palette[31]); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, palette[31]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, palette[28]); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, palette[28]);
DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, palette[16]); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, palette[16]);
@ -155,39 +145,15 @@ int main() {
} }
} }
DrawTexture(mouse, mouseX(), mouseY(), WHITE); DrawTexture(mouse, mouseX(), mouseY(), WHITE);
DrawText(to_string(mouseX()).c_str(), 2, 70, 5, palette[37]); DrawText(std::to_string(mouseX()).c_str(), 2, 70, 5, palette[37]);
DrawText(to_string(mouseY()).c_str(), 2, 78, 5, palette[27]); DrawText(std::to_string(mouseY()).c_str(), 2, 78, 5, palette[27]);
EndTextureMode(); EndTextureMode();
if(flip){
// Get texture data
Image img = LoadImageFromTexture(virtualScreen.texture);
Color* pixels = LoadImageColors(img);
// Modify pixel data
for (int y = 0; y < img.height; y++) {
for (int x = 0; x < img.width; x++) {
// Example: Invert colors
Color pixel = pixels[y * img.width + x];
pixels[y * img.width + x] = { (unsigned char)(255 - pixel.g), (unsigned char)(255 - pixel.r), (unsigned char)(255 - pixel.b), pixel.a };
}
}
// Update texture with modified data
UpdateTexture(virtualScreen.texture, pixels);
}
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE); DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE);
EndDrawing(); EndDrawing();
} }
}
CloseWindow();
delete vm;
return 0;
}

43
src/Pycron.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <fstream>
#include <algorithm>
#include "raylib.h"
#include "pocketpy.h"
class Pycron {
public:
const int virtualScreenWidth = 360;
const int virtualScreenHeight = 203;
const int initialScale = 3;
int startupScreenWidth = virtualScreenWidth * initialScale;
int startupScreenHeight = virtualScreenHeight * initialScale;
int screenWidth = startupScreenWidth;
int screenHeight = startupScreenHeight;
Rectangle virtualScreenBounds;
Vector2 origin;
std::vector<Color> palette;
RenderTexture2D virtualScreen;
Rectangle sourceRec;
Texture2D mouse;
bool shouldClose = false;
pkpy::VM* vm;
Pycron();
~Pycron();
void calculateBounds();
void StartGameLoop();
Color ColorFromHex(int hexValue);
void loadPalette(std::string& path);
int mouseX();
int mouseY();
};

9
src/main.cpp Normal file
View File

@ -0,0 +1,9 @@
using namespace std;
#include "Pycron.h"
int main() {
Pycron pycron_object;
pycron_object.StartGameLoop();
return 0;
}