Refactor
This commit is contained in:
parent
4bdb4fca29
commit
4c35be70df
@ -25,7 +25,11 @@ set(PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/sources/") # Define PROJECT_INCLU
|
|||||||
|
|
||||||
add_executable(UnnamedMicroPythonConsole src/main.cpp
|
add_executable(UnnamedMicroPythonConsole src/main.cpp
|
||||||
src/Pycron.cpp
|
src/Pycron.cpp
|
||||||
src/Pycron.h)
|
src/Pycron.h
|
||||||
|
src/Utilities.cpp
|
||||||
|
src/Utilities.h
|
||||||
|
src/Graphics/Graphics.cpp
|
||||||
|
src/Graphics/Graphics.h)
|
||||||
|
|
||||||
add_subdirectory(dependencies/pocketpy)
|
add_subdirectory(dependencies/pocketpy)
|
||||||
# Declaring our executable
|
# Declaring our executable
|
||||||
|
|||||||
119
src/Graphics/Graphics.cpp
Normal file
119
src/Graphics/Graphics.cpp
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
//
|
||||||
|
// Created by Bobby Lucero on 4/20/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include "Graphics.h"
|
||||||
|
#include "../Utilities.h"
|
||||||
|
|
||||||
|
Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screenWidth(screenWidth), screenHeight(screenHeight){
|
||||||
|
startupScreenWidth = screenWidth * startupScale;
|
||||||
|
startupScreenHeight = screenHeight * startupScale;
|
||||||
|
windowWidth = startupScreenWidth;
|
||||||
|
windowHeight = startupScreenHeight;
|
||||||
|
|
||||||
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||||
|
InitWindow(startupScreenWidth, startupScreenHeight, "test");
|
||||||
|
SetTargetFPS(60);
|
||||||
|
virtualScreen = LoadRenderTexture(screenWidth, screenHeight);
|
||||||
|
virtualScreenLocalBounds = {0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height };
|
||||||
|
calculateScreenPositionInWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Graphics::draw() {
|
||||||
|
|
||||||
|
windowShouldClose = WindowShouldClose();
|
||||||
|
|
||||||
|
if (IsWindowResized()) {
|
||||||
|
windowWidth = GetScreenWidth();
|
||||||
|
windowHeight = GetScreenHeight();
|
||||||
|
calculateScreenPositionInWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
BeginTextureMode(virtualScreen);
|
||||||
|
//////////
|
||||||
|
ClearBackground(palette[1]);
|
||||||
|
DrawText(("Hello World " + std::to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE);
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawCircleLines(mouseX(), mouseY(),3, palette[18]);
|
||||||
|
//////////
|
||||||
|
EndTextureMode();
|
||||||
|
|
||||||
|
renderVirtualScreen();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Graphics::renderVirtualScreen() {
|
||||||
|
BeginDrawing();
|
||||||
|
ClearBackground(palette[0]);
|
||||||
|
DrawTexturePro(virtualScreen.texture, virtualScreenLocalBounds, virtualScreenWindowBounds, origin, 0.0f, WHITE);
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Graphics::loadPalette(std::string &path) {
|
||||||
|
std::ifstream paletteFile(path);
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
if(paletteFile.is_open()){
|
||||||
|
while(getline(paletteFile, line)){
|
||||||
|
palette.push_back(Utilities::ColorFromHex(stoi(line, nullptr, 16)));
|
||||||
|
}
|
||||||
|
paletteFile.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Graphics::calculateScreenPositionInWindow() {
|
||||||
|
float virtualAspectRatio = (float)screenWidth / (float)screenHeight;
|
||||||
|
float windowAspectRatio = (float)windowWidth / (float)windowHeight;
|
||||||
|
|
||||||
|
if(windowAspectRatio > virtualAspectRatio) {
|
||||||
|
virtualScreenWindowBounds.height = (float)windowHeight;
|
||||||
|
virtualScreenWindowBounds.width = virtualScreenWindowBounds.height * virtualAspectRatio;
|
||||||
|
origin.x = -(windowWidth / 2.0f - (virtualScreenWindowBounds.width / 2.0f));
|
||||||
|
origin.y = 0;
|
||||||
|
}else {
|
||||||
|
virtualScreenWindowBounds.width = (float)windowWidth;
|
||||||
|
virtualScreenWindowBounds.height = virtualScreenWindowBounds.width / virtualAspectRatio;
|
||||||
|
origin.x = 0;
|
||||||
|
origin.y = -(windowHeight / 2.0f - (virtualScreenWindowBounds.height / 2.0f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Graphics::mouseX() {
|
||||||
|
float x = GetMouseX();
|
||||||
|
float adjX = x + origin.x;
|
||||||
|
return (int)(adjX / virtualScreenWindowBounds.width * screenWidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Graphics::mouseY() {
|
||||||
|
float y = GetMouseY();
|
||||||
|
float adjY = y + origin.y;
|
||||||
|
return (int)(adjY / virtualScreenWindowBounds.height * screenHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Graphics::toggleFullScreen() {
|
||||||
|
if (IsWindowFullscreen()) {
|
||||||
|
ToggleFullscreen();
|
||||||
|
SetWindowSize(startupScreenWidth, startupScreenHeight);
|
||||||
|
windowWidth = startupScreenWidth;
|
||||||
|
windowHeight = startupScreenHeight;
|
||||||
|
} else {
|
||||||
|
int monitor = GetCurrentMonitor();
|
||||||
|
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
|
||||||
|
windowWidth = GetMonitorWidth(monitor);
|
||||||
|
windowHeight = GetMonitorHeight(monitor);
|
||||||
|
ToggleFullscreen();
|
||||||
|
}
|
||||||
|
calculateScreenPositionInWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
44
src/Graphics/Graphics.h
Normal file
44
src/Graphics/Graphics.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "raylib.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
class Graphics {
|
||||||
|
|
||||||
|
private:
|
||||||
|
// initial spawn size, used to exit full screen as well
|
||||||
|
int startupScreenWidth;
|
||||||
|
int startupScreenHeight;
|
||||||
|
// current size
|
||||||
|
int windowWidth;
|
||||||
|
int windowHeight;
|
||||||
|
|
||||||
|
Rectangle virtualScreenWindowBounds; // size of rect texture on window
|
||||||
|
Vector2 origin; // position of rect texture on window
|
||||||
|
Rectangle virtualScreenLocalBounds; // virtual screen bounds
|
||||||
|
RenderTexture2D virtualScreen; // actual pixel screen
|
||||||
|
|
||||||
|
std::vector<Color> palette;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void renderVirtualScreen();
|
||||||
|
void calculateScreenPositionInWindow();
|
||||||
|
|
||||||
|
public:
|
||||||
|
// virtual screen
|
||||||
|
int screenWidth;
|
||||||
|
int screenHeight;
|
||||||
|
|
||||||
|
bool windowShouldClose;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Graphics(int screenWidth, int screenHeight, int startupScale);
|
||||||
|
|
||||||
|
void draw();
|
||||||
|
|
||||||
|
void loadPalette(std::string& path);
|
||||||
|
int mouseX();
|
||||||
|
int mouseY();
|
||||||
|
void toggleFullScreen();
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
135
src/Pycron.cpp
135
src/Pycron.cpp
@ -2,24 +2,17 @@
|
|||||||
// Created by Bobby Lucero on 4/20/24.
|
// Created by Bobby Lucero on 4/20/24.
|
||||||
//
|
//
|
||||||
#include "Pycron.h"
|
#include "Pycron.h"
|
||||||
|
#include "Utilities.h"
|
||||||
|
#include "Graphics/Graphics.h"
|
||||||
|
|
||||||
Pycron::Pycron() {
|
Pycron::Pycron() {
|
||||||
SetTraceLogLevel(LOG_ERROR);
|
SetTraceLogLevel(LOG_ERROR);
|
||||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
|
||||||
InitWindow(startupScreenWidth, startupScreenHeight, "test");
|
graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale};
|
||||||
SetTargetFPS(60);
|
graphics->loadPalette((std::string &) "../resources/palette2.hex");
|
||||||
virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
|
|
||||||
sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height };
|
|
||||||
calculateBounds();
|
|
||||||
|
|
||||||
vm = new pkpy::VM();
|
vm = new pkpy::VM();
|
||||||
|
|
||||||
// Texture2D logo = LoadTexture("../resources/img.png");
|
|
||||||
mouse = LoadTexture("../resources/mouse.png");
|
|
||||||
//
|
|
||||||
//
|
|
||||||
loadPalette((std::string &) "../resources/palette2.hex");
|
|
||||||
|
|
||||||
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);
|
||||||
pkpy::PyObject* obj = vm->_exec(code, vm->_main);
|
pkpy::PyObject* obj = vm->_exec(code, vm->_main);
|
||||||
@ -29,131 +22,21 @@ Pycron::Pycron() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HideCursor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Pycron::~Pycron(){
|
Pycron::~Pycron(){
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
delete vm;
|
delete vm;
|
||||||
|
delete graphics;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
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 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 nar = (float)screenWidth / (float)screenHeight;
|
|
||||||
|
|
||||||
if(nar > ar) {
|
|
||||||
virtualScreenBounds.height = screenHeight;
|
|
||||||
virtualScreenBounds.width = virtualScreenBounds.height * ar;
|
|
||||||
origin.x = -(screenWidth / 2.0f - (virtualScreenBounds.width / 2.0f));
|
|
||||||
origin.y = 0;
|
|
||||||
}else {
|
|
||||||
virtualScreenBounds.width = screenWidth;
|
|
||||||
virtualScreenBounds.height = virtualScreenBounds.width / ar;
|
|
||||||
origin.x = 0;
|
|
||||||
origin.y = -(screenHeight / 2.0f - (virtualScreenBounds.height / 2.0f));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Pycron::StartGameLoop() {
|
void Pycron::StartGameLoop() {
|
||||||
while (!shouldClose) {
|
|
||||||
|
|
||||||
|
|
||||||
if (IsWindowResized() && !IsWindowFullscreen()) {
|
|
||||||
screenWidth = GetScreenWidth();
|
|
||||||
screenHeight = GetScreenHeight();
|
|
||||||
calculateBounds();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
while (!graphics->windowShouldClose) {
|
||||||
if (IsKeyPressed(KEY_F)) {
|
if (IsKeyPressed(KEY_F)) {
|
||||||
if (IsWindowFullscreen()) {
|
graphics->toggleFullScreen();
|
||||||
ToggleFullscreen();
|
|
||||||
SetWindowSize(startupScreenWidth, startupScreenHeight);
|
|
||||||
screenWidth = startupScreenWidth;
|
|
||||||
screenHeight = startupScreenHeight;
|
|
||||||
} else {
|
|
||||||
int monitor = GetCurrentMonitor();
|
|
||||||
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
|
|
||||||
screenWidth = GetMonitorWidth(monitor);
|
|
||||||
screenHeight = GetMonitorHeight(monitor);
|
|
||||||
ToggleFullscreen();
|
|
||||||
}
|
|
||||||
calculateBounds();
|
|
||||||
}else if(IsKeyPressed(KEY_ESCAPE)){
|
|
||||||
shouldClose = true;
|
|
||||||
}
|
}
|
||||||
|
graphics->draw();
|
||||||
BeginTextureMode(virtualScreen);
|
|
||||||
|
|
||||||
ClearBackground(palette[1]);
|
|
||||||
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);
|
|
||||||
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, 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(std::to_string(mouseX()).c_str(), 2, 70, 5, palette[37]);
|
|
||||||
DrawText(std::to_string(mouseY()).c_str(), 2, 78, 5, palette[27]);
|
|
||||||
|
|
||||||
EndTextureMode();
|
|
||||||
|
|
||||||
BeginDrawing();
|
|
||||||
ClearBackground(BLACK);
|
|
||||||
DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE);
|
|
||||||
EndDrawing();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
29
src/Pycron.h
29
src/Pycron.h
@ -1,43 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <fstream>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include "raylib.h"
|
|
||||||
#include "pocketpy.h"
|
#include "pocketpy.h"
|
||||||
|
#include "Graphics/Graphics.h"
|
||||||
|
|
||||||
class Pycron {
|
class Pycron {
|
||||||
|
|
||||||
public:
|
private:
|
||||||
const int virtualScreenWidth = 360;
|
const int virtualScreenWidth = 360;
|
||||||
const int virtualScreenHeight = 203;
|
const int virtualScreenHeight = 203;
|
||||||
const int initialScale = 3;
|
const int initialScale = 3;
|
||||||
|
Graphics* graphics;
|
||||||
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;
|
pkpy::VM* vm;
|
||||||
|
|
||||||
|
public:
|
||||||
Pycron();
|
Pycron();
|
||||||
~Pycron();
|
~Pycron();
|
||||||
|
|
||||||
void calculateBounds();
|
|
||||||
void StartGameLoop();
|
void StartGameLoop();
|
||||||
Color ColorFromHex(int hexValue);
|
|
||||||
void loadPalette(std::string& path);
|
|
||||||
|
|
||||||
int mouseX();
|
|
||||||
int mouseY();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
21
src/Utilities.cpp
Normal file
21
src/Utilities.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Created by Bobby Lucero on 4/20/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Utilities.h"
|
||||||
|
namespace Utilities {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/Utilities.h
Normal file
7
src/Utilities.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
namespace Utilities {
|
||||||
|
Color ColorFromHex(int hexValue);
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user