clang-format on /apps/demos/bunnymark files
This commit is contained in:
parent
78034b69f5
commit
5c91423fbb
@ -180,7 +180,7 @@ function(give_options_without_warnings target)
|
|||||||
-Bsymbolic-functions
|
-Bsymbolic-functions
|
||||||
-Wl,--hash-style=gnu)
|
-Wl,--hash-style=gnu)
|
||||||
|
|
||||||
target_compile_definitions(${target} PUBLIC
|
target_compile_definitions(${target} PRIVATE
|
||||||
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>)
|
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
@ -6,108 +6,105 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define GRAY ((Color){ 130,130,130,255 })
|
#define GRAY ((Color){130, 130, 130, 255})
|
||||||
#define BLACK ((Color){ 0,0,0,255 })
|
#define BLACK ((Color){0, 0, 0, 255})
|
||||||
|
|
||||||
#define LEFT_CLICK_ADD 20
|
#define LEFT_CLICK_ADD 20
|
||||||
#define RIGHT_CLICK_ADD 500
|
#define RIGHT_CLICK_ADD 500
|
||||||
|
|
||||||
|
|
||||||
void handle_input(void){
|
void handle_input(void)
|
||||||
|
{
|
||||||
State *state = ctx.udata;
|
State *state = ctx.udata;
|
||||||
if(ctx.input.mouse_state == 1 && ctx.input.mouse_window_position.y > 60){ // Left click
|
if (ctx.input.mouse_state == 1 && ctx.input.mouse_window_position.y > 60)
|
||||||
|
{ // Left click
|
||||||
for (int i = 0; i < LEFT_CLICK_ADD; i++)
|
for (int i = 0; i < LEFT_CLICK_ADD; i++)
|
||||||
|
{
|
||||||
|
if (state->bunniesCount < MAX_BUNNIES)
|
||||||
{
|
{
|
||||||
if (state->bunniesCount < MAX_BUNNIES)
|
state->bunnies[state->bunniesCount].position =
|
||||||
{
|
(Vec2){(float)ctx.input.mouse_window_position.x, (float)ctx.input.mouse_window_position.y};
|
||||||
state->bunnies[state->bunniesCount].position = (Vec2){
|
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
|
||||||
(float)ctx.input.mouse_window_position.x,
|
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
|
||||||
(float)ctx.input.mouse_window_position.y
|
state->bunnies[state->bunniesCount].color =
|
||||||
};
|
(Color){rand() % 190 + 50, rand() % 160 + 80, rand() % 140 + 100, 255};
|
||||||
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
|
state->bunniesCount++;
|
||||||
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
|
|
||||||
state->bunnies[state->bunniesCount].color = (Color){
|
|
||||||
rand() % 190 + 50,
|
|
||||||
rand() % 160 + 80,
|
|
||||||
rand() % 140 + 100,
|
|
||||||
255
|
|
||||||
};
|
|
||||||
state->bunniesCount++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(ctx.input.mouse_state == 4){ // Right click
|
|
||||||
|
if (ctx.input.mouse_state == 4)
|
||||||
|
{ // Right click
|
||||||
for (int i = 0; i < RIGHT_CLICK_ADD; i++)
|
for (int i = 0; i < RIGHT_CLICK_ADD; i++)
|
||||||
|
{
|
||||||
|
if (state->bunniesCount < MAX_BUNNIES)
|
||||||
{
|
{
|
||||||
if (state->bunniesCount < MAX_BUNNIES)
|
state->bunnies[state->bunniesCount].position =
|
||||||
{
|
(Vec2){(float)ctx.input.mouse_window_position.x, (float)ctx.input.mouse_window_position.y};
|
||||||
state->bunnies[state->bunniesCount].position = (Vec2){
|
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
|
||||||
(float)ctx.input.mouse_window_position.x,
|
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
|
||||||
(float)ctx.input.mouse_window_position.y
|
state->bunnies[state->bunniesCount].color =
|
||||||
};
|
(Color){rand() % 190 + 50, rand() % 160 + 80, rand() % 140 + 100, 255};
|
||||||
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0;
|
state->bunniesCount++;
|
||||||
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0;
|
|
||||||
state->bunnies[state->bunniesCount].color = (Color){
|
|
||||||
rand() % 190 + 50,
|
|
||||||
rand() % 160 + 80,
|
|
||||||
rand() % 140 + 100,
|
|
||||||
255
|
|
||||||
};
|
|
||||||
state->bunniesCount++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_tick(void) {
|
void game_tick(void)
|
||||||
|
{
|
||||||
static char bunny_count_text[64];
|
static char bunny_count_text[64];
|
||||||
static char bunny_path[64] = "wabbit_alpha.png";
|
static char bunny_path[64] = "wabbit_alpha.png";
|
||||||
|
|
||||||
// State *state = ctx.udata;
|
// State *state = ctx.udata;
|
||||||
if (ctx.initialization_needed) { // First tick, initalizing data
|
if (ctx.initialization_needed)
|
||||||
|
{ // First tick, initalizing data
|
||||||
// Allocating State struct to store data there
|
// Allocating State struct to store data there
|
||||||
if (!ctx.udata) ctx.udata = ccalloc(1, sizeof(State));
|
if (!ctx.udata)
|
||||||
|
ctx.udata = ccalloc(1, sizeof(State));
|
||||||
((State *)ctx.udata)->bunniesCount = 0;
|
((State *)ctx.udata)->bunniesCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
State *state = ctx.udata;
|
State *state = ctx.udata;
|
||||||
|
|
||||||
const double delta = (double)(ctx.delta_time) / 1000.0; // Receiving floating point delta value (diving by 1000 based on vibe)
|
const double delta =
|
||||||
|
(double)(ctx.delta_time) / 1000.0; // Receiving floating point delta value (diving by 1000 based on vibe)
|
||||||
|
|
||||||
for (int i = 0; i < state->bunniesCount; i++){
|
for (int i = 0; i < state->bunniesCount; i++)
|
||||||
state->bunnies[i].position.x += state->bunnies[i].speed.x;
|
{
|
||||||
state->bunnies[i].position.y += state->bunnies[i].speed.y;
|
state->bunnies[i].position.x += state->bunnies[i].speed.x;
|
||||||
|
state->bunnies[i].position.y += state->bunnies[i].speed.y;
|
||||||
|
|
||||||
if (((state->bunnies[i].position.x + BUNNY_W/2) > ctx.window_w) ||
|
if (((state->bunnies[i].position.x + BUNNY_W / 2) > ctx.window_w) ||
|
||||||
((state->bunnies[i].position.x + BUNNY_W/2) < 0)) state->bunnies[i].speed.x *= -1;
|
((state->bunnies[i].position.x + BUNNY_W / 2) < 0))
|
||||||
if (((state->bunnies[i].position.y + BUNNY_H/2) > ctx.window_h) ||
|
state->bunnies[i].speed.x *= -1;
|
||||||
((state->bunnies[i].position.y + BUNNY_H/2 - 60) < 0)) state->bunnies[i].speed.y *= -1;
|
if (((state->bunnies[i].position.y + BUNNY_H / 2) > ctx.window_h) ||
|
||||||
|
((state->bunnies[i].position.y + BUNNY_H / 2 - 60) < 0))
|
||||||
|
state->bunnies[i].speed.y *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_input();
|
handle_input();
|
||||||
|
|
||||||
// Clear window with Gray color (set the background color this way)
|
// Clear window with Gray color (set the background color this way)
|
||||||
draw_rectangle((Rect){0,0, ctx.window_w, ctx.window_h}, GRAY);
|
draw_rectangle((Rect){0, 0, ctx.window_w, ctx.window_h}, GRAY);
|
||||||
|
|
||||||
for (int i = 0; i < state->bunniesCount; i++){ // Draw each bunny based on their position and color, also scale accordingly
|
for (int i = 0; i < state->bunniesCount; i++)
|
||||||
m_sprite(
|
{ // Draw each bunny based on their position and color, also scale accordingly
|
||||||
m_set(path, bunny_path),
|
m_sprite(m_set(path, bunny_path),
|
||||||
m_set(rect, ((Rect){ .x = (int)state->bunnies[i].position.x, .y = (int)state->bunnies[i].position.y,
|
m_set(rect, ((Rect){.x = (int)state->bunnies[i].position.x,
|
||||||
.w = BUNNY_W * SPRITE_SCALE, .h = BUNNY_H * SPRITE_SCALE
|
.y = (int)state->bunnies[i].position.y,
|
||||||
})),
|
.w = BUNNY_W * SPRITE_SCALE,
|
||||||
m_opt(color, (state->bunnies[i].color)),
|
.h = BUNNY_H * SPRITE_SCALE})),
|
||||||
m_opt(stretch, true),
|
m_opt(color, (state->bunnies[i].color)), m_opt(stretch, true), );
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// Formatting text to display, might want to add FPS here too
|
// Formatting text to display, might want to add FPS here too
|
||||||
snprintf(bunny_count_text, 64, "Bunnies: %d", state->bunniesCount);
|
snprintf(bunny_count_text, 64, "Bunnies: %d", state->bunniesCount);
|
||||||
|
|
||||||
draw_text(bunny_count_text, (Vec2){ 0, 0 }, 40, BLACK, "/fonts/kenney-pixel.ttf");
|
draw_text(bunny_count_text, (Vec2){0, 0}, 40, BLACK, "/fonts/kenney-pixel.ttf");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void game_end(void)
|
||||||
void game_end(void) {
|
{
|
||||||
State *state = ctx.udata;
|
State *state = ctx.udata;
|
||||||
|
|
||||||
// Free state when game ends
|
// Free state when game ends
|
||||||
|
@ -3,23 +3,23 @@
|
|||||||
|
|
||||||
#include "twn_game_api.h"
|
#include "twn_game_api.h"
|
||||||
|
|
||||||
#define MAX_BUNNIES 50000 // 50K bunnies limit
|
#define MAX_BUNNIES 100000 // 100K bunnies limit
|
||||||
#define BUNNY_W 26
|
#define BUNNY_W 26
|
||||||
#define BUNNY_H 37
|
#define BUNNY_H 37
|
||||||
#define SPRITE_SCALE 1
|
#define SPRITE_SCALE 1
|
||||||
|
|
||||||
typedef struct Bunny {
|
typedef struct Bunny
|
||||||
|
{
|
||||||
Vec2 position;
|
Vec2 position;
|
||||||
Vec2 speed;
|
Vec2 speed;
|
||||||
Color color;
|
Color color;
|
||||||
} Bunny;
|
} Bunny;
|
||||||
|
|
||||||
typedef struct State {
|
typedef struct State
|
||||||
|
{
|
||||||
Bunny bunnies[MAX_BUNNIES];
|
Bunny bunnies[MAX_BUNNIES];
|
||||||
int bunniesCount;
|
int bunniesCount;
|
||||||
InputState mouse_state;
|
InputState mouse_state;
|
||||||
} State;
|
} State;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -44,8 +44,8 @@ typedef struct ActionHashItem {
|
|||||||
} ActionHashItem;
|
} ActionHashItem;
|
||||||
|
|
||||||
|
|
||||||
|
/* TODO: don't assume SDL button mask */
|
||||||
typedef struct InputState {
|
typedef struct InputState {
|
||||||
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
|
|
||||||
uint32_t mouse_state; /* SDL mouse button bitmask */
|
uint32_t mouse_state; /* SDL mouse button bitmask */
|
||||||
Vec2i mouse_window_position;
|
Vec2i mouse_window_position;
|
||||||
Vec2i mouse_relative_position;
|
Vec2i mouse_relative_position;
|
||||||
@ -54,6 +54,7 @@ typedef struct InputState {
|
|||||||
|
|
||||||
/* engine state */
|
/* engine state */
|
||||||
ActionHashItem *action_hash;
|
ActionHashItem *action_hash;
|
||||||
|
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
|
||||||
} InputState;
|
} InputState;
|
||||||
|
|
||||||
|
|
||||||
|
@ -245,6 +245,8 @@ void use_texture_mode(TextureMode mode) {
|
|||||||
VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) {
|
VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, bytes, NULL, GL_STREAM_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, bytes, NULL, GL_STREAM_DRAW);
|
||||||
|
if (bytes == 0)
|
||||||
|
SDL_TriggerBreakpoint();
|
||||||
void *mapping = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
void *mapping = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||||
if (!mapping)
|
if (!mapping)
|
||||||
CRY("build_vertex_buffer", "Error mapping a vertex array buffer");
|
CRY("build_vertex_buffer", "Error mapping a vertex array buffer");
|
||||||
@ -282,7 +284,6 @@ void finally_render_sprites(const Primitive2D primitives[],
|
|||||||
{
|
{
|
||||||
/* TODO: maybe do, dunno */
|
/* TODO: maybe do, dunno */
|
||||||
// glBindBuffer(GL_VERTEX_ARRAY, vertex_buffer);
|
// glBindBuffer(GL_VERTEX_ARRAY, vertex_buffer);
|
||||||
(void)buffer;
|
|
||||||
|
|
||||||
GLsizei off;
|
GLsizei off;
|
||||||
GLsizei voff;
|
GLsizei voff;
|
||||||
|
@ -70,7 +70,6 @@ void clear_draw_buffer(void) {
|
|||||||
(1.0f / 255) * 230,
|
(1.0f / 255) * 230,
|
||||||
(1.0f / 255) * 230, 1);
|
(1.0f / 255) * 230, 1);
|
||||||
|
|
||||||
/* TODO: don't clear color when skybox is applied? */
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT |
|
glClear(GL_COLOR_BUFFER_BIT |
|
||||||
GL_DEPTH_BUFFER_BIT |
|
GL_DEPTH_BUFFER_BIT |
|
||||||
GL_STENCIL_BUFFER_BIT);
|
GL_STENCIL_BUFFER_BIT);
|
||||||
|
@ -138,6 +138,7 @@ void render_sprites(const Primitive2D primitives[],
|
|||||||
const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
|
const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
|
||||||
const SpritePrimitive sprite = primitives[cur].sprite;
|
const SpritePrimitive sprite = primitives[cur].sprite;
|
||||||
|
|
||||||
|
/* TODO: try caching it */
|
||||||
const Rect srcrect =
|
const Rect srcrect =
|
||||||
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
|
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user