Added raylib, drawing bounds and tests

This commit is contained in:
Bobby Lucero 2024-04-17 22:05:28 -07:00
parent b3d6996437
commit 50ed165578
3 changed files with 124 additions and 2 deletions

View File

@ -3,12 +3,43 @@ project(UnnamedMicroPythonConsole)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
# Adding Raylib
include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
set(BUILD_GAMES OFF CACHE BOOL "" FORCE) # don't build the supplied example games
FetchContent_Declare(
raylib
GIT_REPOSITORY "https://github.com/raysan5/raylib.git"
GIT_TAG "master"
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(raylib)
# Adding our source files
file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/sources/*.c") # Define PROJECT_SOURCES as a list of all source files
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 main.cpp)
add_subdirectory(dependencies/pocketpy) add_subdirectory(dependencies/pocketpy)
target_link_libraries(UnnamedMicroPythonConsole pocketpy) # Declaring our executable
target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE})
target_link_libraries(${PROJECT_NAME} PRIVATE raylib pocketpy)
# Setting ASSETS_PATH
target_compile_definitions(${PROJECT_NAME} PUBLIC ASSETS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/assets/") # Set the asset path macro to the absolute path on the dev machine
if(EMSCRIPTEN) if(EMSCRIPTEN)
# exceptions must be enabled for emscripten # exceptions must be enabled for emscripten
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fexceptions") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fexceptions")
endif() endif()
file(COPY ${CMAKE_SOURCE_DIR}/resources DESTINATION ${CMAKE_BINARY_DIR})

View File

@ -1,10 +1,101 @@
#include <algorithm>
#include "pocketpy.h" #include "pocketpy.h"
#include "raylib.h"
using namespace std;
const int virtualScreenWidth = 240;
const int virtualScreenHeight = 136;
int screenWidth = virtualScreenWidth * 4;
int screenHeight = virtualScreenHeight * 4;
void calculateBounds(Rectangle& bounds, Vector2& origin) {
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));
origin.y = 0;
}else {
bounds.width = screenWidth;
bounds.height = bounds.width / ar;
origin.x = 0;
origin.y = -(screenHeight / 2.0f - (bounds.height / 2.0f));
}
}
int main() { int main() {
const float virtualRatio = (float)screenWidth/(float)virtualScreenWidth;
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(screenWidth, screenHeight, "test");
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);
pkpy::VM* vm = new pkpy::VM(); pkpy::VM* vm = new pkpy::VM();
vm->exec("print('hello world')");
Texture2D logo = LoadTexture("resources/img.png");
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) {
}
while(!WindowShouldClose()) {
if (IsWindowResized() && !IsWindowFullscreen())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
calculateBounds(virtualScreenBounds, origin);
}
if (IsKeyPressed(KEY_F)) {
int monitor = GetCurrentMonitor();
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
calculateBounds(virtualScreenBounds, origin);
ToggleFullscreen();
}
BeginTextureMode(virtualScreen);
ClearBackground(GRAY);
DrawText("Hello World", 5, 5, 5, RAYWHITE);
DrawTexture(logo, 5, 20, 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);
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE);
EndDrawing();
}
CloseWindow();
delete vm;
return 0; return 0;
} }

BIN
resources/img.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 503 B