/
redgpu
/
raygpu
Обзор
Документация
Войти
/
redgpu
/
raygpu
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/input_keys.c
131 строка
5 KB
manuel5975p
Web Keyinput
02 янв 2025, 12:45
02 янв 2025, 12:45
496280c
Код
Авторство
О чём код?
#include <raygpu.h> #ifdef __EMSCRIPTEN__ #include <emscripten.h> #endif #define MAX_BUILDINGS 100 Rectangle player = { 400, 550, 40, 40 }; Rectangle buildings[MAX_BUILDINGS] = { 0 }; Color buildColors[MAX_BUILDINGS] = { 0 }; Camera2D camera; const int screenWidth = 1200; const int screenHeight = 720; void mainloop(void){ // Update //---------------------------------------------------------------------------------- // Player movement if (IsKeyDown(KEY_RIGHT)) player.x += 2; else if (IsKeyDown(KEY_LEFT)) player.x -= 2; // Camera target follows player camera.target = (Vector2){ player.x + 20, player.y + 20 }; // Camera rotation controls if (IsKeyDown(KEY_A)) camera.rotation--; else if (IsKeyDown(KEY_S)) camera.rotation++; // Limit camera rotation to 80 degrees (-40 to 40) if (camera.rotation > 40) camera.rotation = 40; else if (camera.rotation < -40) camera.rotation = -40; // Camera zoom controls camera.zoom += ((float)GetMouseWheelMove()*0.05f); if (camera.zoom > 3.0f) camera.zoom = 3.0f; else if (camera.zoom < 0.1f) camera.zoom = 0.1f; // Camera reset (zoom and rotation) if (IsKeyPressed(KEY_R)) { camera.zoom = 1.0f; camera.rotation = 0.0f; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode2D(camera); DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY); for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]); DrawRectangleRec(player, RED); DrawLine((int)camera.target.x, -screenHeight*10, (int)camera.target.x, screenHeight*10, GREEN); DrawLine(-screenWidth*10, (int)camera.target.y, screenWidth*10, (int)camera.target.y, GREEN); EndMode2D(); DrawText("SCREEN AREA", 940, 10, 30, RED); DrawRectangle(0, 0, screenWidth, 5, RED); DrawRectangle(0, 5, 5, screenHeight - 10, RED); DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED); DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED); DrawRectangle( 10, 10, 850, 210, Fade(SKYBLUE, 0.5f)); DrawRectangleLines( 10, 10, 850, 210, BLUE); DrawText("Free 2d camera controls:", 20, 20, 30, BLACK); DrawText("- Right/Left to move Offset", 40, 60, 30, DARKGRAY); DrawText("- Mouse Wheel to Zoom in-out", 40, 100, 30, DARKGRAY); DrawText("- A / S to Rotate", 40, 140, 30, DARKGRAY); DrawText("- R to reset Zoom and Rotation", 40, 180, 30, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ int main(void) { // Initialization //-------------------------------------------------------------------------------------- InitWindow(screenWidth, screenHeight, "Key Input Example"); int spacing = 0; for (int i = 0; i < MAX_BUILDINGS; i++) { buildings[i].width = (float)GetRandomValue(50, 200); buildings[i].height = (float)GetRandomValue(100, 800); buildings[i].y = screenHeight - 130.0f - buildings[i].height; buildings[i].x = -6000.0f + spacing; spacing += (int)buildings[i].width; buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 }; } camera.target = (Vector2){ player.x + 20.0f, player.y + 20.0f }; camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f }; camera.rotation = 0.0f; camera.zoom = 1.0f; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop #ifndef __EMSCRIPTEN__ while(!WindowShouldClose()){ mainloop(); } #else emscripten_set_main_loop(mainloop, 0, 0); #endif // De-Initialization //-------------------------------------------------------------------------------------- //CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; }