Skip to content

Instantly share code, notes, and snippets.

@powerc9000
Created March 10, 2020 01:52
Show Gist options
  • Save powerc9000/484f47cad017384c65965aa45fefe8f4 to your computer and use it in GitHub Desktop.
Save powerc9000/484f47cad017384c65965aa45fefe8f4 to your computer and use it in GitHub Desktop.
package raylib
RAYLIB_KEYS :: enum {
// Alphanumeric keys
KEY_APOSTROPHE = 39,
KEY_COMMA = 44,
KEY_MINUS = 45,
KEY_PERIOD = 46,
KEY_SLASH = 47,
KEY_ZERO = 48,
KEY_ONE = 49,
KEY_TWO = 50,
KEY_THREE = 51,
KEY_FOUR = 52,
KEY_FIVE = 53,
KEY_SIX = 54,
KEY_SEVEN = 55,
KEY_EIGHT = 56,
KEY_NINE = 57,
KEY_SEMICOLON = 59,
KEY_EQUAL = 61,
KEY_A = 65,
KEY_B = 66,
KEY_C = 67,
KEY_D = 68,
KEY_E = 69,
KEY_F = 70,
KEY_G = 71,
KEY_H = 72,
KEY_I = 73,
KEY_J = 74,
KEY_K = 75,
KEY_L = 76,
KEY_M = 77,
KEY_N = 78,
KEY_O = 79,
KEY_P = 80,
KEY_Q = 81,
KEY_R = 82,
KEY_S = 83,
KEY_T = 84,
KEY_U = 85,
KEY_V = 86,
KEY_W = 87,
KEY_X = 88,
KEY_Y = 89,
KEY_Z = 90,
// Function keys
KEY_SPACE = 32,
KEY_ESCAPE = 256,
KEY_ENTER = 257,
KEY_TAB = 258,
KEY_BACKSPACE = 259,
KEY_INSERT = 260,
KEY_DELETE = 261,
KEY_RIGHT = 262,
KEY_LEFT = 263,
KEY_DOWN = 264,
KEY_UP = 265,
KEY_PAGE_UP = 266,
KEY_PAGE_DOWN = 267,
KEY_HOME = 268,
KEY_END = 269,
KEY_CAPS_LOCK = 280,
KEY_SCROLL_LOCK = 281,
KEY_NUM_LOCK = 282,
KEY_PRINT_SCREEN = 283,
KEY_PAUSE = 284,
KEY_F1 = 290,
KEY_F2 = 291,
KEY_F3 = 292,
KEY_F4 = 293,
KEY_F5 = 294,
KEY_F6 = 295,
KEY_F7 = 296,
KEY_F8 = 297,
KEY_F9 = 298,
KEY_F10 = 299,
KEY_F11 = 300,
KEY_F12 = 301,
KEY_LEFT_SHIFT = 340,
KEY_LEFT_CONTROL = 341,
KEY_LEFT_ALT = 342,
KEY_LEFT_SUPER = 343,
KEY_RIGHT_SHIFT = 344,
KEY_RIGHT_CONTROL = 345,
KEY_RIGHT_ALT = 346,
KEY_RIGHT_SUPER = 347,
KEY_KB_MENU = 348,
KEY_LEFT_BRACKET = 91,
KEY_BACKSLASH = 92,
KEY_RIGHT_BRACKET = 93,
KEY_GRAVE = 96,
// Keypad keys
KEY_KP_0 = 320,
KEY_KP_1 = 321,
KEY_KP_2 = 322,
KEY_KP_3 = 323,
KEY_KP_4 = 324,
KEY_KP_5 = 325,
KEY_KP_6 = 326,
KEY_KP_7 = 327,
KEY_KP_8 = 328,
KEY_KP_9 = 329,
KEY_KP_DECIMAL = 330,
KEY_KP_DIVIDE = 331,
KEY_KP_MULTIPLY = 332,
KEY_KP_SUBTRACT = 333,
KEY_KP_ADD = 334,
KEY_KP_ENTER = 335,
KEY_KP_EQUAL = 336
};
package main
import "raylib"
MAX_BUILDINGS :: 200;
main :: proc() {
using raylib;
using raylib.RAYLIB_KEYS;
raylib.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Initializatioa
//--------------------------------------------------------------------------------------
screenWidth := 800;
screenHeight := 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
player: Rectangle = { 400, 280, 40, 40 };
buildings: [MAX_BUILDINGS]Rectangle = { };
buildColors: [MAX_BUILDINGS]Color = { };
spacing : f32 = 0.0;
for i in 0..<MAX_BUILDINGS {
buildings[i].width = cast(f32)GetRandomValue(50, 200);
buildings[i].height = cast(f32)GetRandomValue(100, 800);
buildings[i].y = cast(f32)screenHeight - 130.0 - buildings[i].height;
buildings[i].x = -6000.0 + spacing;
spacing += buildings[i].width;
buildColors[i] = (Color){ cast(u8)GetRandomValue(200, 240), cast(u8)GetRandomValue(200, 240), cast(u8)GetRandomValue(200, 250), 255 };
}
camera: Camera2D = { };
camera.target = { x=player.x + 20, y=player.y + 20 };
camera.offset = { cast(f32)screenWidth/2, cast(f32)screenHeight/2 };
camera.rotation = 0.0;
camera.zoom = 1.0;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
for !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 = { player.x + 20, player.y + 20 };
// Camera rotation controls
if (IsKeyDown(KEY_A)) {
camera.rotation -= 1;
}
else if (IsKeyDown(KEY_S)) {
camera.rotation += 1;
}
// 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 += (cast(f32)GetMouseWheelMove()*0.05);
if (camera.zoom > 3.0) {
camera.zoom = 3.0;
}
else if (camera.zoom < 0.1) {
camera.zoom = 0.1;
}
// Camera reset (zoom and rotation)
if (IsKeyPressed(KEY_R))
{
camera.zoom = 1.0;
camera.rotation = 0.0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(COLOR_RAYWHITE);
BeginMode2D(camera);
DrawRectangle(-6000, 320, 13000, 8000, COLOR_DARKGRAY);
for i in 0..<MAX_BUILDINGS {
DrawRectangleRec(buildings[i], buildColors[i]);
}
DrawRectangleRec(player, COLOR_RED);
DrawLine(cast(int)camera.target.x, cast(int)-screenHeight*10, cast(int)camera.target.x, cast(int)screenHeight*10, COLOR_GREEN);
DrawLine(cast(int)-screenWidth*10, cast(int)camera.target.y, cast(int)screenWidth*10, cast(int)camera.target.y, COLOR_GREEN);
EndMode2D();
DrawText("SCREEN AREA", 640, 10, 20, COLOR_RED);
DrawRectangle(0, 0, screenWidth, 5, COLOR_RED);
DrawRectangle(0, 5, 5, screenHeight - 10, COLOR_RED);
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, COLOR_RED);
DrawRectangle(0, screenHeight - 5, screenWidth, 5, COLOR_RED);
DrawRectangleLines( 10, 10, 250, 113, COLOR_BLUE);
DrawText("Free 2d camera controls:", 20, 20, 10, COLOR_BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, COLOR_DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, COLOR_DARKGRAY);
DrawText("- A / S to Rotate", 40, 80, 10, COLOR_DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, COLOR_DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
}
package raylib
import "core:fmt";
when ODIN_OS == "darwin" {
foreign import raylib {
"libraylib.a",
"system:Cocoa.framework",
"system:IOKit.framework",
"system:CoreVideo.framework",
"system:GLUT.framework",
"system:OpenGL.framework",
}
};
import _c "core:c";
Color:: struct {
r: u8,
g: u8,
b: u8,
a: u8
};
Vector2 :: struct #packed {
x: _c.float,
y: _c.float
};
Camera2D :: struct #packed {
offset : Vector2,
target : Vector2,
rotation : _c.float,
zoom : _c.float,
};
Texture2D :: struct #packed {
id : uint,
width : int,
height : int,
mipmaps : int,
format : int,
};
Rectangle :: struct #packed {
x : _c.float,
y : _c.float,
width : _c.float,
height : _c.float,
};
CharInfo :: struct #packed {
value : int,
rec : Rectangle,
offset_x : int,
offset_y : int,
advance_x : int,
data : ^u8,
};
Font :: struct {
texture : Texture2D,
base_size : int,
chars_count : int,
chars : ^CharInfo,
};
COLOR_LIGHTGRAY := Color { 200, 200, 200, 255 };
COLOR_GRAY := Color { 130, 130, 130, 255 };
COLOR_DARKGRAY := Color { 80, 80, 80, 255 };
COLOR_YELLOW := Color { 253, 249, 0, 255 };
COLOR_GOLD := Color { 255, 203, 0, 255 };
COLOR_ORANGE := Color { 255, 161, 0, 255 };
COLOR_PINK := Color { 255, 109, 194, 255 };
COLOR_RED := Color { 230, 41, 55, 255 };
COLOR_MAROON := Color { 190, 33, 55, 255 };
COLOR_GREEN := Color { 0, 228, 48, 255 };
COLOR_LIME := Color { 0, 158, 47, 255 };
COLOR_DARKGREEN := Color { 0, 117, 44, 255 };
COLOR_SKYBLUE := Color { 102, 191, 255, 255 };
COLOR_BLUE := Color { 0, 121, 241, 255 };
COLOR_DARKBLUE := Color { 0, 82, 172, 255 };
COLOR_PURPLE := Color { 200, 122, 255, 255 };
COLOR_VIOLET := Color { 135, 60, 190, 255 };
COLOR_DARKPURPLE := Color { 112, 31, 126, 255 };
COLOR_BEIGE := Color { 211, 176, 131, 255 };
COLOR_BROWN := Color { 127, 106, 79, 255 };
COLOR_DARKBROWN := Color { 76, 63, 47, 255 };
COLOR_WHITE := Color { 255, 255, 255, 255 };
COLOR_BLACK := Color { 0, 0, 0, 255 };
COLOR_BLANK := Color { 0, 0, 0, 0 };
COLOR_MAGENTA := Color { 255, 0, 255, 255 };
COLOR_RAYWHITE := Color { 245, 245, 245, 255 };
foreign raylib {
InitWindow :: proc (width: int, height: int, title: cstring) -> i32 ---;
BeginDrawing :: proc () ---;
EndDrawing :: proc () ---;
ClearBackground:: proc (Color) ---;
SetTargetFPS :: proc (int) ---;
DrawText :: proc (cstring, int, int, int, Color) ---;
DrawTextEx :: proc (Font, cstring, Vector2, f32, f32, Color) ---;
CloseWindow :: proc () ---;
WindowShouldClose :: proc () -> bool ---;
IsKeyDown :: proc (key: RAYLIB_KEYS) -> bool ---;
IsKeyPressed :: proc (key: RAYLIB_KEYS) -> bool ---;
GetFrameTime :: proc () -> f32 ---;
BeginMode2D :: proc "c" (Camera2D) ---;
EndMode2D :: proc() ---;
GetFontDefault :: proc() -> Font ---;
DrawRectangleRec :: proc(Rectangle, Color) ---;
DrawRectangle :: proc "c" (int, int, int, int, Color) ---;
SetTraceLogLevel :: proc "c" (int) --- ;
GetRandomValue :: proc(int, int) -> int ---;
GetMouseWheelMove :: proc() -> int ---;
DrawLine :: proc(int, int, int, int, Color) ---;
DrawRectangleLines :: proc (int, int, int, int, Color) ---;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment