mirror of
https://gitlab.com/raylibtemplates/rt.git
synced 2026-07-11 00:46:23 -04:00
Reimplement C++ into C
This commit is contained in:
130
src/main.c
130
src/main.c
@@ -1,77 +1,97 @@
|
|||||||
#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);
|
||||||
float scale = MIN((float)GetScreenWidth() / SCREEN_WIDTH,
|
#else
|
||||||
(float)GetScreenHeight() / SCREEN_HEIGHT);
|
while (!WindowShouldClose())
|
||||||
|
MainLoop();
|
||||||
cube.transform =
|
#endif
|
||||||
MatrixMultiply(MatrixRotateXYZ(Vector3Scale((Vector3){1.0f, 1.5f, 2.5f},
|
|
||||||
GetFrameTime())),
|
|
||||||
cube.transform);
|
|
||||||
|
|
||||||
BeginTextureMode(target);
|
|
||||||
{
|
|
||||||
ClearBackground(BLACK);
|
|
||||||
BeginMode3D(camera);
|
|
||||||
{
|
|
||||||
DrawModel(cube, Vector3Zero(), 1.0f, WHITE);
|
|
||||||
}
|
|
||||||
EndMode3D();
|
|
||||||
}
|
|
||||||
EndTextureMode();
|
|
||||||
|
|
||||||
BeginDrawing();
|
|
||||||
{
|
|
||||||
ClearBackground(BLACK);
|
|
||||||
DrawTexturePro(
|
|
||||||
target.texture,
|
|
||||||
(Rectangle){0.0f, 0.0f, (float)target.texture.width,
|
|
||||||
(float)-target.texture.height},
|
|
||||||
(Rectangle){
|
|
||||||
(GetScreenWidth() - ((float)SCREEN_WIDTH * scale)) * 0.5f,
|
|
||||||
(GetScreenHeight() - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
|
|
||||||
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale},
|
|
||||||
(Vector2){0, 0}, 0.0f, WHITE);
|
|
||||||
}
|
|
||||||
EndDrawing();
|
|
||||||
}
|
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainLoop() {
|
||||||
|
int winWidth = GetScreenWidth(), winHeight = GetScreenHeight();
|
||||||
|
float scale =
|
||||||
|
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
|
||||||
|
|
||||||
|
cube.transform =
|
||||||
|
MatrixMultiply(MatrixRotateXYZ(Vector3Scale((Vector3){1.0f, 1.5f, 2.5f},
|
||||||
|
GetFrameTime())),
|
||||||
|
cube.transform);
|
||||||
|
|
||||||
|
BeginTextureMode(target);
|
||||||
|
{
|
||||||
|
ClearBackground(BLACK);
|
||||||
|
DrawCircleV((Vector2){160.0f, 120.0f}, 120.0f, WHITE);
|
||||||
|
BeginMode3D(cam);
|
||||||
|
{
|
||||||
|
DrawModel(cube, Vector3Zero(), 1.0f, WHITE);
|
||||||
|
}
|
||||||
|
EndMode3D();
|
||||||
|
}
|
||||||
|
EndTextureMode();
|
||||||
|
|
||||||
|
BeginDrawing();
|
||||||
|
{
|
||||||
|
ClearBackground(BLACK);
|
||||||
|
DrawTexturePro(
|
||||||
|
target.texture,
|
||||||
|
(Rectangle){0.0f, 0.0f, (float)SCREEN_WIDTH, (float)-SCREEN_HEIGHT},
|
||||||
|
(Rectangle){(winWidth - ((float)SCREEN_WIDTH * scale)) * 0.5f,
|
||||||
|
(winHeight - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
|
||||||
|
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale},
|
||||||
|
(Vector2){0, 0}, 0.0f, WHITE);
|
||||||
|
}
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user