7
0
mirror of https://gitlab.com/raylibtemplates/rt.git synced 2026-07-11 08:46:27 -04:00

Reimplement C++ into C

This commit is contained in:
Maple Redleaf
2025-12-19 11:53:51 -06:00
parent 4ec432602f
commit 651fb47cd4

View File

@@ -1,43 +1,69 @@
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#include <math.h>
#include <stdio.h>
#include "../data/cube.png.h" #include "../data/cube.png.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#define SCREEN_WIDTH (320) #define SCREEN_WIDTH (320)
#define SCREEN_HEIGHT (240) #define SCREEN_HEIGHT (240)
#define WIN_WIDTH (1280)
#define WIN_HEIGHT (720)
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
int main(void) { void MainLoop();
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_WINDOW_RESIZABLE |
FLAG_WINDOW_MAXIMIZED | FLAG_VSYNC_HINT);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "game");
SetWindowMinSize(320, 240);
SetTargetFPS(60);
RenderTexture2D target = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT); RenderTexture2D target;
Image texImg;
Texture2D texture;
Camera3D cam;
Model cube;
int main(void) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
InitWindow(WIN_WIDTH, WIN_HEIGHT, "game");
SetTargetFPS(60);
SetExitKey(KEY_BACKSPACE);
target = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
SetTextureFilter(target.texture, TEXTURE_FILTER_POINT); SetTextureFilter(target.texture, TEXTURE_FILTER_POINT);
Image image = texImg = LoadImageFromMemory(".png", cube_png, cube_png_len);
LoadImageFromMemory(".png", assets_cube_png, assets_cube_png_len); texture = LoadTextureFromImage(texImg);
Texture2D texture = LoadTextureFromImage(image); cam = (Camera3D){0};
cam.position = (Vector3){0.0f, 0.0f, sqrtf(3.0f)};
cam.target = (Vector3){0.0f, 0.0f, 0.0f};
cam.up = (Vector3){0.0f, 1.0f, 0.0f};
cam.fovy = 60.0f;
cam.projection = CAMERA_PERSPECTIVE;
Camera camera = {0}; cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
camera.position = (Vector3){0.0f, 0.0f, 5.0f};
camera.target = (Vector3){0.0f, 0.0f, 0.0f};
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
camera.fovy = 60.0f;
camera.projection = CAMERA_PERSPECTIVE;
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
cube.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texture; cube.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texture;
while (!WindowShouldClose()) { #if defined(PLATFORM_WEB)
emscripten_set_main_loop(MainLoop, 0, 1);
#else
while (!WindowShouldClose())
MainLoop();
#endif
CloseWindow();
float scale = MIN((float)GetScreenWidth() / SCREEN_WIDTH, return 0;
(float)GetScreenHeight() / SCREEN_HEIGHT); }
void MainLoop() {
int winWidth = GetScreenWidth(), winHeight = GetScreenHeight();
float scale =
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
cube.transform = cube.transform =
MatrixMultiply(MatrixRotateXYZ(Vector3Scale((Vector3){1.0f, 1.5f, 2.5f}, MatrixMultiply(MatrixRotateXYZ(Vector3Scale((Vector3){1.0f, 1.5f, 2.5f},
@@ -47,7 +73,8 @@ int main(void) {
BeginTextureMode(target); BeginTextureMode(target);
{ {
ClearBackground(BLACK); ClearBackground(BLACK);
BeginMode3D(camera); DrawCircleV((Vector2){160.0f, 120.0f}, 120.0f, WHITE);
BeginMode3D(cam);
{ {
DrawModel(cube, Vector3Zero(), 1.0f, WHITE); DrawModel(cube, Vector3Zero(), 1.0f, WHITE);
} }
@@ -60,18 +87,11 @@ int main(void) {
ClearBackground(BLACK); ClearBackground(BLACK);
DrawTexturePro( DrawTexturePro(
target.texture, target.texture,
(Rectangle){0.0f, 0.0f, (float)target.texture.width, (Rectangle){0.0f, 0.0f, (float)SCREEN_WIDTH, (float)-SCREEN_HEIGHT},
(float)-target.texture.height}, (Rectangle){(winWidth - ((float)SCREEN_WIDTH * scale)) * 0.5f,
(Rectangle){ (winHeight - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
(GetScreenWidth() - ((float)SCREEN_WIDTH * scale)) * 0.5f,
(GetScreenHeight() - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale}, (float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale},
(Vector2){0, 0}, 0.0f, WHITE); (Vector2){0, 0}, 0.0f, WHITE);
} }
EndDrawing(); EndDrawing();
}
CloseWindow();
return 0;
} }