#include <SDL2/SDL.h>#include <SDL2/SDL_mixer.h> #include "playground.h"#include "basis.h"#include "loader.h"#include "prefs.h"#include "simulation.h" /*Copyright (c) 2013-2021 Devine Lu Linvega2018 web port by rezmason2021 C89 port by rezmason Permission to use, copy, modify, and distribute this software for anypurpose with or without fee is hereby granted, provided that the abovecopyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIESWITH REGARD TO THIS SOFTWARE.*/ #define DEFAULT_WINDOW_WIDTH 1280#define DEFAULT_WINDOW_HEIGHT 720#define CHUNK_SIZE 512#define ORG_NAME "hundredrabbits"#define APP_NAME "playground" #define MUSIC_CHANNEL 0#define AMBIENCE_CHANNEL 1#define RESERVED_CHANNELS 2 #define SIM_DT 2#define NUM_SEALS 41#define PARTICLE_SCALE 1 typedef struct { int windowW, windowH; Particle particles[NUM_SEALS]; int loaded;} GameState; static GameState state;static int portraitMode = 0;static int reset = 0;static double lastCollision = 0; static SDL_Cursor *testCursor = NULL;static char *sealPaths[NUM_SEALS] = { "graphics/interface/seal.antechannel.svg", "graphics/interface/seal.capsule.svg", "graphics/interface/seal.circular.svg", "graphics/interface/seal.entente.svg", "graphics/interface/seal.forest.svg", "graphics/interface/seal.metamondst.svg", "graphics/interface/seal.nataniev.svg", "graphics/interface/seal.nether.svg", "graphics/interface/seal.none.svg", "graphics/interface/seal.rainre.svg", "graphics/interface/seal.stones.svg", "graphics/interface/seal.studio.svg", "graphics/interface/seal.terminal.svg", "graphics/interface/alert.svg", "graphics/interface/clock.0.svg", "graphics/interface/clock.1.svg", "graphics/interface/clock.2.svg", "graphics/interface/dimclock.shadow.svg", "graphics/interface/dimclock.state0.svg", "graphics/interface/dimclock.state1.svg", "graphics/interface/dimclock.state2.svg", "graphics/interface/entente.x.left.svg", "graphics/interface/entente.x.right.svg", "graphics/interface/entente.x.straight.svg", "graphics/interface/entente.y.left.svg", "graphics/interface/entente.y.right.svg", "graphics/interface/entente.y.straight.svg", "graphics/interface/footstep.svg", "graphics/interface/fuse.0.svg", "graphics/interface/fuse.1.svg", "graphics/interface/fuse.2.svg", "graphics/interface/fuse.3.svg", "graphics/interface/music.off.svg", "graphics/interface/music.on.svg", "graphics/interface/progress.act1.svg", "graphics/interface/progress.act2.svg", "graphics/interface/progress.act3.svg", "graphics/interface/progress.act4.svg", "graphics/interface/progress.act5.svg", "graphics/interface/progress.entente.svg", "graphics/interface/save.svg"};static Image *seals[NUM_SEALS];static Sound *music = NULL;static Sound *ambience = NULL;static Sound *sound = NULL;static SDL_Rect particleRect;static SDL_Point particleCenter; static intloadAssets(void){ int success = 1; int i; Loader_Init(Basis_GetRenderer()); for(i = 0; i < NUM_SEALS; i++) { success &= Loader_LoadImage(Loader_CreateImage(&seals[i], LOADTYPE_LAZY, sealPaths[i])); } success &= Loader_LoadCursor(&testCursor, "graphics/cursor/pointer.up.svg"); success &= Loader_LoadSound(Loader_CreateSound(&music, "audio/record/music_credit.mp3", 1)); success &= Loader_LoadSound(Loader_CreateSound(&ambience, "audio/ambience/ambient_forest.mp3", 1)); success &= Loader_LoadSound(Loader_CreateSound(&sound, "audio/effect/footstep_collide.mp3", 1)); if(!success) { printf("Asset loading failed.\n"); } return success;} static intquit(void){ int i; Sim_Quit(); for(i = 0; i < NUM_SEALS; i++) { Loader_DestroyImage(seals[i]); seals[i] = NULL; } SDL_FreeCursor(testCursor); testCursor = NULL; Loader_DestroySound(music); music = NULL; Loader_DestroySound(ambience); ambience = NULL; Loader_DestroySound(sound); sound = NULL; Loader_Quit(); Prefs_Quit(); Basis_Quit(); return 0;} static voidprocessWindowResize(int w, int h){ state.windowW = w; state.windowH = h; Sim_Resize(state.windowW, state.windowH);} static voidprocessEvent(SDL_Event *event){ switch(event->type) { case SDL_KEYDOWN: switch(event->key.keysym.sym) { case SDLK_a: case SDLK_LEFT: printf("LEFT\n"); break; case SDLK_d: case SDLK_RIGHT: printf("RIGHT\n"); break; case SDLK_w: case SDLK_UP: printf("UP\n"); break; case SDLK_s: case SDLK_DOWN: printf("DOWN\n"); break; } break; case SDL_MOUSEMOTION: Sim_UpdateDrag(event->motion.x, event->motion.y); break; case SDL_MOUSEBUTTONDOWN: Sim_BeginDrag(state.particles, NUM_SEALS, event->button.x, event->button.y); break; case SDL_MOUSEBUTTONUP: Sim_EndDrag(); break; }} static voidupdate(double time, double deltaTime){ if(Sim_Update(state.particles, NUM_SEALS, deltaTime) && time - lastCollision > 0.1) { lastCollision = time; Mix_PlayChannel(-1, sound->chunk, 0); }} static voidrender(SDL_Renderer *renderer){ int i; SDL_RenderClear(renderer); for(i = 0; i < NUM_SEALS; i++) { Particle *p = &state.particles[i]; particleRect.x = p->x; particleRect.y = p->y; SDL_SetTextureColorMod(seals[i]->texture, p->r, p->g, p->b); SDL_SetTextureAlphaMod(seals[i]->texture, 0xFF /*p->a*/); SDL_RenderCopyEx(renderer, seals[i]->texture, NULL, &particleRect, p->angle, &particleCenter, 0); } SDL_RenderPresent(renderer);} static voidprocessArgs(int argc, char *argv[]){ int i; for(i = 1; i < argc; i++) { if(strcmp(argv[i], "-portrait") == 0) { portraitMode = 1; } if(strcmp(argv[i], "-reset") == 0) { reset = 1; } }} intPlayground_Run(int argc, char *argv[]){ int particleRadius; processArgs(argc, argv); Prefs_Init(ORG_NAME, APP_NAME, &state, sizeof(state)); if(reset) { Prefs_Reset(); } else { Prefs_Load(); } if(!state.loaded) { state.windowW = portraitMode ? DEFAULT_WINDOW_HEIGHT : DEFAULT_WINDOW_WIDTH; state.windowH = portraitMode ? DEFAULT_WINDOW_WIDTH : DEFAULT_WINDOW_HEIGHT; } if(!Basis_Init("Playground", CHUNK_SIZE, &state.windowW, &state.windowH, !state.loaded) || !loadAssets()) { return quit(); } particleRadius = seals[0]->w / 2 * PARTICLE_SCALE; particleCenter.x = particleRadius; particleCenter.y = particleRadius; particleRect.w = particleRadius * 2; particleRect.h = particleRadius * 2; Sim_Init(state.windowW, state.windowH, particleRadius, SIM_DT); if(!state.loaded) { Sim_Randomize(state.particles, NUM_SEALS); } state.loaded = 1; SDL_SetCursor(testCursor); Mix_ReserveChannels(RESERVED_CHANNELS); Mix_Volume(AMBIENCE_CHANNEL, 0.2 * MIX_MAX_VOLUME); Mix_PlayChannel(AMBIENCE_CHANNEL, ambience->chunk, -1); Mix_PlayChannel(MUSIC_CHANNEL, music->chunk, -1); Basis_Run(processEvent, processWindowResize, update, render); Prefs_Save(); return quit();}
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include "playground.h"
#include "basis.h"
#include "loader.h"
#include "prefs.h"
#include "simulation.h"
/*
Copyright (c) 2013-2021 Devine Lu Linvega
2018 web port by rezmason
2021 C89 port by rezmason
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
#define DEFAULT_WINDOW_WIDTH 1280
#define DEFAULT_WINDOW_HEIGHT 720
#define CHUNK_SIZE 512
#define ORG_NAME "hundredrabbits"
#define APP_NAME "playground"
#define MUSIC_CHANNEL 0
#define AMBIENCE_CHANNEL 1
#define RESERVED_CHANNELS 2
#define SIM_DT 2
#define NUM_SEALS 41
#define PARTICLE_SCALE 1
typedef struct {
int windowW, windowH;
Particle particles[NUM_SEALS];
int loaded;
} GameState;
static GameState state;
static int portraitMode = 0;
static int reset = 0;
static double lastCollision = 0;
static SDL_Cursor *testCursor = NULL;
static char *sealPaths[NUM_SEALS] = {
"graphics/interface/seal.antechannel.svg",
"graphics/interface/seal.capsule.svg",
"graphics/interface/seal.circular.svg",
"graphics/interface/seal.entente.svg",
"graphics/interface/seal.forest.svg",
"graphics/interface/seal.metamondst.svg",
"graphics/interface/seal.nataniev.svg",
"graphics/interface/seal.nether.svg",
"graphics/interface/seal.none.svg",
"graphics/interface/seal.rainre.svg",
"graphics/interface/seal.stones.svg",
"graphics/interface/seal.studio.svg",
"graphics/interface/seal.terminal.svg",
"graphics/interface/alert.svg",
"graphics/interface/clock.0.svg",
"graphics/interface/clock.1.svg",
"graphics/interface/clock.2.svg",
"graphics/interface/dimclock.shadow.svg",
"graphics/interface/dimclock.state0.svg",
"graphics/interface/dimclock.state1.svg",
"graphics/interface/dimclock.state2.svg",
"graphics/interface/entente.x.left.svg",
"graphics/interface/entente.x.right.svg",
"graphics/interface/entente.x.straight.svg",
"graphics/interface/entente.y.left.svg",
"graphics/interface/entente.y.right.svg",
"graphics/interface/entente.y.straight.svg",
"graphics/interface/footstep.svg",
"graphics/interface/fuse.0.svg",
"graphics/interface/fuse.1.svg",
"graphics/interface/fuse.2.svg",
"graphics/interface/fuse.3.svg",
"graphics/interface/music.off.svg",
"graphics/interface/music.on.svg",
"graphics/interface/progress.act1.svg",
"graphics/interface/progress.act2.svg",
"graphics/interface/progress.act3.svg",
"graphics/interface/progress.act4.svg",
"graphics/interface/progress.act5.svg",
"graphics/interface/progress.entente.svg",
"graphics/interface/save.svg"};
static Image *seals[NUM_SEALS];
static Sound *music = NULL;
static Sound *ambience = NULL;
static Sound *sound = NULL;
static SDL_Rect particleRect;
static SDL_Point particleCenter;
static int
loadAssets(void)
{
int success = 1;
int i;
Loader_Init(Basis_GetRenderer());
for(i = 0; i < NUM_SEALS; i++) {
success &= Loader_LoadImage(Loader_CreateImage(&seals[i], LOADTYPE_LAZY, sealPaths[i]));
}
success &= Loader_LoadCursor(&testCursor, "graphics/cursor/pointer.up.svg");
success &= Loader_LoadSound(Loader_CreateSound(&music, "audio/record/music_credit.mp3", 1));
success &= Loader_LoadSound(Loader_CreateSound(&ambience, "audio/ambience/ambient_forest.mp3", 1));
success &= Loader_LoadSound(Loader_CreateSound(&sound, "audio/effect/footstep_collide.mp3", 1));
if(!success) {
printf("Asset loading failed.\n");
}
return success;
}
static int
quit(void)
{
int i;
Sim_Quit();
for(i = 0; i < NUM_SEALS; i++) {
Loader_DestroyImage(seals[i]);
seals[i] = NULL;
}
SDL_FreeCursor(testCursor);
testCursor = NULL;
Loader_DestroySound(music);
music = NULL;
Loader_DestroySound(ambience);
ambience = NULL;
Loader_DestroySound(sound);
sound = NULL;
Loader_Quit();
Prefs_Quit();
Basis_Quit();
return 0;
}
static void
processWindowResize(int w, int h)
{
state.windowW = w;
state.windowH = h;
Sim_Resize(state.windowW, state.windowH);
}
static void
processEvent(SDL_Event *event)
{
switch(event->type) {
case SDL_KEYDOWN:
switch(event->key.keysym.sym) {
case SDLK_a:
case SDLK_LEFT:
printf("LEFT\n");
break;
case SDLK_d:
case SDLK_RIGHT:
printf("RIGHT\n");
break;
case SDLK_w:
case SDLK_UP:
printf("UP\n");
break;
case SDLK_s:
case SDLK_DOWN:
printf("DOWN\n");
break;
}
break;
case SDL_MOUSEMOTION:
Sim_UpdateDrag(event->motion.x, event->motion.y);
break;
case SDL_MOUSEBUTTONDOWN:
Sim_BeginDrag(state.particles, NUM_SEALS, event->button.x, event->button.y);
break;
case SDL_MOUSEBUTTONUP:
Sim_EndDrag();
break;
}
}
static void
update(double time, double deltaTime)
{
if(Sim_Update(state.particles, NUM_SEALS, deltaTime) && time - lastCollision > 0.1) {
lastCollision = time;
Mix_PlayChannel(-1, sound->chunk, 0);
}
}
static void
render(SDL_Renderer *renderer)
{
int i;
SDL_RenderClear(renderer);
for(i = 0; i < NUM_SEALS; i++) {
Particle *p = &state.particles[i];
particleRect.x = p->x;
particleRect.y = p->y;
SDL_SetTextureColorMod(seals[i]->texture, p->r, p->g, p->b);
SDL_SetTextureAlphaMod(seals[i]->texture, 0xFF /*p->a*/);
SDL_RenderCopyEx(renderer, seals[i]->texture, NULL, &particleRect, p->angle, &particleCenter, 0);
}
SDL_RenderPresent(renderer);
}
static void
processArgs(int argc, char *argv[])
{
int i;
for(i = 1; i < argc; i++) {
if(strcmp(argv[i], "-portrait") == 0) {
portraitMode = 1;
}
if(strcmp(argv[i], "-reset") == 0) {
reset = 1;
}
}
}
int
Playground_Run(int argc, char *argv[])
{
int particleRadius;
processArgs(argc, argv);
Prefs_Init(ORG_NAME, APP_NAME, &state, sizeof(state));
if(reset) {
Prefs_Reset();
} else {
Prefs_Load();
}
if(!state.loaded) {
state.windowW = portraitMode ? DEFAULT_WINDOW_HEIGHT : DEFAULT_WINDOW_WIDTH;
state.windowH = portraitMode ? DEFAULT_WINDOW_WIDTH : DEFAULT_WINDOW_HEIGHT;
}
if(!Basis_Init("Playground", CHUNK_SIZE, &state.windowW, &state.windowH, !state.loaded) ||
!loadAssets()) {
return quit();
}
particleRadius = seals[0]->w / 2 * PARTICLE_SCALE;
particleCenter.x = particleRadius;
particleCenter.y = particleRadius;
particleRect.w = particleRadius * 2;
particleRect.h = particleRadius * 2;
Sim_Init(state.windowW, state.windowH, particleRadius, SIM_DT);
if(!state.loaded) {
Sim_Randomize(state.particles, NUM_SEALS);
}
state.loaded = 1;
SDL_SetCursor(testCursor);
Mix_ReserveChannels(RESERVED_CHANNELS);
Mix_Volume(AMBIENCE_CHANNEL, 0.2 * MIX_MAX_VOLUME);
Mix_PlayChannel(AMBIENCE_CHANNEL, ambience->chunk, -1);
Mix_PlayChannel(MUSIC_CHANNEL, music->chunk, -1);
Basis_Run(processEvent, processWindowResize, update, render);
Prefs_Save();
return quit();
}