Skip to content

Instantly share code, notes, and snippets.

@panta
Created March 21, 2022 11:48
Show Gist options
  • Save panta/96228f5c4c4ce81bddffbdaa126ca40a to your computer and use it in GitHub Desktop.
Save panta/96228f5c4c4ce81bddffbdaa126ca40a to your computer and use it in GitHub Desktop.
Simple raylib example which draws lines on the screen.
/*
* raylib-lines.c
*
* To compile:
* clang -Wall $(pkg-config --cflags raylib) -o raylib-lines raylib-lines.c $(pkg-config --libs raylib)
*
* Copyright (C) 2022 Marco Pantaleoni.
*/
#include <time.h>
#include <stdlib.h>
#include "raylib.h"
#define N_COLORS 20
// for raylib, see:
// - https://www.raylib.com/cheatsheet/cheatsheet.html
// - https://github.com/raysan5/raylib/wiki
int main(void) {
const int screenWidth = 800;
const int screenHeight = 600;
InitWindow(screenWidth, screenHeight, "drawing lines");
SetTargetFPS(60); // Set our "game" to run at 60 frames-per-second
Image offscreen = GenImageColor(screenWidth, screenHeight, RAYWHITE);
Color colors[N_COLORS] = { 0 };
for (int i = 0; i < N_COLORS; i++) colors[i] = (Color){ GetRandomValue(10, 250), GetRandomValue(10, 150), GetRandomValue(10, 100), 255 };
// Main "game" loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
ImageDrawLine(&offscreen,
GetRandomValue(10, offscreen.width-10), GetRandomValue(10, offscreen.height-10),
GetRandomValue(10, offscreen.width-10), GetRandomValue(10, offscreen.height-10),
colors[GetRandomValue(0, N_COLORS-1)]);
Texture2D texture = LoadTextureFromImage(offscreen); // Image converted to texture, uploaded to GPU memory (VRAM)
// UnloadImage(offscreen); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE);
// DrawCircleV(ballPosition, 50, MAROON);
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