Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/libc/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <raylib.h>
#include <raymath.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void raylib_js_set_entry(void (*entry)(void));

const double time_per_calc = 1; // in s
double time_since_last_calc = 0;
char buf[BUFSIZ];
int counter = 3;

void GameFrame()
{
BeginDrawing();

ClearBackground((Color){20, 20, 20, 255});

if (counter <= 0)
{
exit(0);
}

const size_t w = GetScreenWidth();
const size_t h = GetScreenHeight();
const size_t font_size = 72;

const int ch = h / 2 - (font_size / 2);

sprintf(buf, "%d%c", counter, '\0');
size_t text_size = MeasureText(buf, font_size);
int cw = w / 2 - (text_size / 2);
DrawText(buf, cw, ch, font_size, RED);

if (time_since_last_calc >= time_per_calc)
{
counter--;
time_since_last_calc = 0;
}
else
{
time_since_last_calc += GetFrameTime();
}

EndDrawing();
}

int main()
{
InitWindow(800, 600, "Hello, with math.h");
SetTargetFPS(60);

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose())
{
GameFrame();
}
CloseWindow();
#endif
return 0;
}
84 changes: 84 additions & 0 deletions examples/libc/file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <raylib.h>
#include <raymath.h>
#include <stdio.h>
#include <stdbool.h>

void raylib_js_set_entry(void (*entry)(void));

bool once = true;
FILE *read_me = NULL;

char buf[BUFSIZ];
const double time_per_calc = 2; // in s
double time_since_last_calc = 0;

void GameFrame()
{
BeginDrawing();

ClearBackground((Color){20, 20, 20, 255});

const size_t w = GetScreenWidth();
const size_t h = GetScreenHeight();
const size_t font_size = 48;

if (time_since_last_calc >= time_per_calc)
{
// Get new line
if (is_file_ready(read_me))
{
if (feof(read_me))
{
rewind(read_me);
}

fgets(buf, BUFSIZ, read_me);
}

time_since_last_calc = 0;
}
else
{
time_since_last_calc += GetFrameTime();
}

// display text somewhat sensibly
size_t current_font_size = font_size;
size_t text_size = MeasureText(buf, font_size);
while (text_size > w && current_font_size > font_size / 2)
{
text_size = MeasureText(buf, --current_font_size);
}
if (text_size <= w)
{
DrawText(buf, w / 2 - (text_size / 2), h / 2 - (current_font_size / 2), current_font_size, RED);
}
else
{
DrawText(buf, 10, h / 2 - (current_font_size / 2), current_font_size, RED);
}

EndDrawing();
}

int main()
{
InitWindow(800, 600, "Hello, with loaded README.md");
SetTargetFPS(60);

read_me = fopen("README.md", "rb");

int w = GetScreenWidth();
int h = GetScreenHeight();

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose())
{
GameFrame();
}
CloseWindow();
#endif
return 0;
}
79 changes: 79 additions & 0 deletions examples/libc/malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <raylib.h>
#include <raymath.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

void raylib_js_set_entry(void (*entry)(void));

const double time_per_calc = 1; // in s
double time_since_last_calc = 0;

char buf[128];

char *buf_dym = NULL;
int counter = 1;

void GameFrame()
{
BeginDrawing();

ClearBackground((Color){20, 20, 20, 255});

const size_t w = GetScreenWidth();
const size_t h = GetScreenHeight();
const size_t font_size = 72;

const int ch = h / 2 - (font_size / 2);

buf_dym = realloc(buf_dym, counter + 1);

memset(buf_dym, 'a', counter);
buf_dym[counter] = '\0';
size_t text_size = MeasureText(buf_dym, font_size);
int cw = w / 2 - (text_size / 2);
DrawText(buf_dym, cw, ch - font_size, font_size, RED);

sprintf(buf, "malloc size: %d%c", counter + 1, '\0');
size_t m_text_size = MeasureText(buf, font_size);
cw = w / 2 - (m_text_size / 2);
DrawText(buf, cw, ch + font_size, font_size, RED);

if (text_size > w)
{
counter = 1;
free(buf_dym);
buf_dym = NULL;
}

if (time_since_last_calc >= time_per_calc)
{
counter++;
time_since_last_calc = 0;
}
else
{
time_since_last_calc += GetFrameTime();
}

EndDrawing();
}

int main()
{
InitWindow(800, 600, "Hello, with malloc");
SetTargetFPS(60);

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose())
{
GameFrame();
}
CloseWindow();
#endif
return 0;
}
69 changes: 69 additions & 0 deletions examples/libc/math.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <raylib.h>
#include <raymath.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void raylib_js_set_entry(void (*entry)(void));

const double time_per_calc = 2; // in s
double time_since_last_calc = 0;
char buf[BUFSIZ];
double random = 0;
void GameFrame()
{
BeginDrawing();

ClearBackground((Color){20, 20, 20, 255});

const size_t w = GetScreenWidth();
const size_t h = GetScreenHeight();
const size_t font_size = 48;

const int ch = h / 2 - (font_size / 2);

sprintf(buf, "cos(%.2g) = %.2g%c", random, cos(random), '\0');
size_t text_size = MeasureText(buf, font_size);
int cw = w / 2 - (text_size / 2);
DrawText(buf, cw, ch - font_size, font_size, RED);

sprintf(buf, "sin(%.2g) = %.2g%c", random, sin(random), '\0');
text_size = MeasureText(buf, font_size);
cw = w / 2 - (text_size / 2);
DrawText(buf, cw, ch + font_size, font_size, RED);

if (time_since_last_calc >= time_per_calc)
{
const int random_int = rand();
random = ((random_int / (double)RAND_MAX) - 0.5) * 2 * 4;

time_since_last_calc = 0;
}
else
{
time_since_last_calc += GetFrameTime();
}

EndDrawing();
}

int main()
{
InitWindow(800, 600, "Hello, with math.h");
SetTargetFPS(60);

int w = GetScreenWidth();
int h = GetScreenHeight();

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose())
{
GameFrame();
}
CloseWindow();
#endif
return 0;
}
51 changes: 51 additions & 0 deletions examples/libc/time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <raylib.h>
#include <raymath.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>

void raylib_js_set_entry(void (*entry)(void));

char buf[BUFSIZ];

void GameFrame()
{
BeginDrawing();

ClearBackground((Color){20, 20, 20, 255});

const size_t w = GetScreenWidth();
const size_t h = GetScreenHeight();
const size_t font_size = 48;

const int ch = h / 2 - (font_size / 2);

time_t t = time(NULL);
struct tm *ts = localtime(&t);

char *time_text = asctime(ts);
sprintf(buf, "%s%c", time_text, '\0');

size_t text_size = MeasureText(buf, font_size);
int cw = w / 2 - (text_size / 2);
DrawText(buf, cw, ch, font_size, RED);

EndDrawing();
}

int main()
{
InitWindow(800, 600, "Hello, with math.h");
SetTargetFPS(60);

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose())
{
GameFrame();
}
CloseWindow();
#endif
return 0;
}
Loading