Skip to content

Instantly share code, notes, and snippets.

@pluckyporcupine
Last active February 8, 2020 23:46
Show Gist options
  • Save pluckyporcupine/bbaa93b00e020c53faf27e5bde087374 to your computer and use it in GitHub Desktop.
Save pluckyporcupine/bbaa93b00e020c53faf27e5bde087374 to your computer and use it in GitHub Desktop.
raylib core_2d_camera example in C-Mera
;; This is more or less a direct translation, so there are no fancy Lisp macros or anything in this code.
(include <raylib.h>)
(defvar MAX_BUILDINGS 100)
(function main () -> int
(decl ((const int screenWidth = 800)
(const int screenHeight = 450)))
(InitWindow screenWidth screenHeight "raylib [core] example - 2d camera")
(decl ((Rectangle player = (clist 400 280 40 40))
(Rectangle buildings[MAX_BUILDINGS] = (clist 0))
(Color buildColors[MAX_BUILDINGS] = (clist 0))
(int spacing = 0)))
(for ((int i = 0) (< i MAX_BUILDINGS) ++i)
(set buildings[i].width (GetRandomValue 50 200)
buildings[i].height (GetRandomValue 100 800)
buildings[i].y (- screenHeight 130 buildings[i].height)
buildings[i].x (+ (- 6000) spacing)
spacing (+ spacing buildings[i].width))
(set buildColors[i] (cast Color (clist (GetRandomValue 200 240) (GetRandomValue 200 240) (GetRandomValue 200 250) 255))))
(decl ((Camera2D camera = (clist 0))))
(set camera.target (cast Vector2 (clist (+ player.x 20) (+ player.y 20)))
camera.offset (cast Vector2 (clist (/ screenWidth 2) (/ screenHeight 2)))
camera.rotation 0.0f
camera.zoom 1.0f)
(SetTargetFPS 60)
(while (!= (WindowShouldClose) 1)
(cond ((== (IsKeyDown KEY_RIGHT) 1)
(set player.x (+ player.x 2)))
((== (IsKeyDown KEY_LEFT) 1)
(set player.x (- player.x 2))))
(set camera.target (cast Vector2 (clist (+ player.x 20) (+ player.y 20))))
(cond ((== (IsKeyDown KEY_A) 1)
(set camera.rotation (- camera.rotation 1)))
((== (IsKeyDown KEY_S) 1)
(set camera.rotation (+ camera.rotation 1))))
(cond ((> camera.rotation 40)
(set camera.rotation 40))
((< camera.rotation (- 40))
(set camera.rotation (- 40))))
(set camera.zoom (+ camera.zoom (* (cast float (GetMouseWheelMove)) 0.05f)))
(cond ((> camera.zoom 3.0f)
(set camera.zoom 3.0f))
((< camera.zoom 0.1f)
(set camera.zoom 0.1f)))
(if (== (IsKeyPressed KEY_R) 1)
(set camera.zoom 1.0f
camera.rotation 0.0f))
(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 camera.target.x (- (* screenHeight 10)) camera.target.x (* screenHeight 10) GREEN)
(DrawLine (- (* screenWidth 10)) camera.target.y (* screenWidth 10) camera.target.y GREEN)
(EndMode2D)
(DrawText "SCREEN AREA" 640 10 20 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 250 113 (Fade SKYBLUE 0.5f))
(DrawRectangleLines 10 10 250 113 BLUE)
(DrawText "Free 2d camera controls:" 20 20 10 BLACK)
(DrawText "- Right/Left to move Offset" 40 40 10 DARKGRAY)
(DrawText "- Mouse Wheel to Zoom in-out" 40 60 10 DARKGRAY)
(DrawText "- A / S to Rotate" 40 80 10 DARKGRAY)
(DrawText "- R to reset Zoom and Rotation" 40 100 10 DARKGRAY)
(EndDrawing))
(CloseWindow)
(return 0))
#include <raylib.h>
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
Rectangle player = { 400, 280, 40, 40 };
Rectangle buildings[100] = { 0 };
Color buildColors[100] = { 0 };
int spacing = 0;
for(int i = 0; i < 100; ++i) {
buildings[i].width = GetRandomValue(50, 200);
buildings[i].height = GetRandomValue(100, 800);
buildings[i].y = screenHeight - 130 - buildings[i].height;
buildings[i].x = -6000 + spacing;
spacing = spacing + buildings[i].width;
buildColors[i] = ((Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 });
}
Camera2D camera = { 0 };
camera.target = ((Vector2){ (player.x + 20), (player.y + 20) });
camera.offset = ((Vector2){ (screenWidth / 2), (screenHeight / 2) });
camera.rotation = 0.00000000e+0f;
camera.zoom = 1.00000000e+0f;
SetTargetFPS(60);
while (WindowShouldClose() != 1) {
if (IsKeyDown(KEY_RIGHT) == 1)
player.x = player.x + 2;
else if (IsKeyDown(KEY_LEFT) == 1)
player.x = player.x - 2;
camera.target = ((Vector2){ (player.x + 20), (player.y + 20) });
if (IsKeyDown(KEY_A) == 1)
camera.rotation = camera.rotation - 1;
else if (IsKeyDown(KEY_S) == 1)
camera.rotation = camera.rotation + 1;
if (camera.rotation > 40)
camera.rotation = 40;
else if (camera.rotation < -40)
camera.rotation = -40;
camera.zoom = camera.zoom + (((float)GetMouseWheelMove()) * 5.00000000e-2f);
if (camera.zoom > 3.00000000e+0f)
camera.zoom = 3.00000000e+0f;
else if (camera.zoom < 1.00000000e-1f)
camera.zoom = 1.00000000e-1f;
if (IsKeyPressed(KEY_R) == 1) {
camera.zoom = 1.00000000e+0f;
camera.rotation = 0.00000000e+0f;
}
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
for(int i = 0; i < 100; ++i) {
DrawRectangleRec(buildings[i], buildColors[i]);
}
DrawRectangleRec(player, RED);
DrawLine(camera.target.x, -(screenHeight * 10), camera.target.x, screenHeight * 10, GREEN);
DrawLine(-(screenWidth * 10), camera.target.y, screenWidth * 10, camera.target.y, GREEN);
EndMode2D();
DrawText("SCREEN AREA", 640, 10, 20, 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, 250, 113, Fade(SKYBLUE, 5.00000000e-1f));
DrawRectangleLines(10, 10, 250, 113, BLUE);
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
/*******************************************************************************************
*
* raylib [core] example - 2d camera
*
* This example has been created using raylib 1.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#define MAX_BUILDINGS 100
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
Rectangle player = { 400, 280, 40, 40 };
Rectangle buildings[MAX_BUILDINGS] = { 0 };
Color buildColors[MAX_BUILDINGS] = { 0 };
int spacing = 0;
for (int i = 0; i < MAX_BUILDINGS; i++)
{
buildings[i].width = GetRandomValue(50, 200);
buildings[i].height = GetRandomValue(100, 800);
buildings[i].y = screenHeight - 130 - buildings[i].height;
buildings[i].x = -6000 + spacing;
spacing += buildings[i].width;
buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
}
Camera2D camera = { 0 };
camera.target = (Vector2){ player.x + 20, player.y + 20 };
camera.offset = (Vector2){ screenWidth/2, screenHeight/2 };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// 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(camera.target.x, -screenHeight*10, camera.target.x, screenHeight*10, GREEN);
DrawLine(-screenWidth*10, camera.target.y, screenWidth*10, camera.target.y, GREEN);
EndMode2D();
DrawText("SCREEN AREA", 640, 10, 20, 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, 250, 113, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 250, 113, BLUE);
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment