~rabbits/hiversaires

c0364487b6077b24dbeaad474ce0aab3ec8d733d — Rezmason 3 years ago ebe7c24
Jettisoning the font support.
6 files changed, 10 insertions(+), 125 deletions(-)

M README.md
M build.sh
M src/basis.c
M src/loader.c
M src/loader.h
M src/playground.c
M README.md => README.md +2 -2
@@ 8,9 8,9 @@ Feel free to send pull requests if you find an issue that you wish to correct.

## Build

We intend for Hiversaires to compile for most platforms. To build Hiversaires, you must have [SDL2](https://wiki.libsdl.org/), [SDL_ttf](https://www.libsdl.org/projects/SDL_ttf/), [SDL_mixer](https://www.libsdl.org/projects/SDL_mixer/) and [mpg123](https://mpg123.org).
We intend for Hiversaires to compile for most platforms. To build Hiversaires, you must have [SDL2](https://wiki.libsdl.org/), [SDL_mixer](https://www.libsdl.org/projects/SDL_mixer/) and [mpg123](https://mpg123.org).

If you're on a Mac, you can use Homebrew to install these libraries: `brew install sdl2 sdl2_ttf sdl2_mixer mpg123`
If you're on a Mac, you can use Homebrew to install these libraries: `brew install sdl2 sdl2_mixer mpg123`

We rebuild the mixer to support MP3s by running `brew edit sdl2_mixer`, replacing `--disable-music-mp3-mpg123` with `--enable-music-mp3-mpg123`,
and then running `brew reinstall --build-from-source sdl2_mixer`

M build.sh => build.sh +2 -2
@@ 6,7 6,7 @@ PROJECT_NAME="hiversaires"

DEFINES=""
OUTPUT_PARAM="-o ./bin/$PROJECT_NAME"
LINK_PARAMS="-L/usr/local/lib -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lm"
LINK_PARAMS="-L/usr/local/lib -lSDL2 -lSDL2_image -lSDL2_mixer -lm"
WARNING_PARAMS="-Wall"
INSTRUMENTATION_PARAMS=""
FLAGS="-Os -g0 -s"


@@ 67,7 67,7 @@ then
  then
    # Copy media
    echo "Copying media..."
    cp -R media/audio media/fonts media/icons media/data bin
    cp -R media/audio media/icons media/data bin
    mkdir bin/graphics
    cp -R media/graphics/cursor media/graphics/interface media/graphics/logos bin/graphics
    mkdir bin/graphics/node_render

M src/basis.c => src/basis.c +0 -6
@@ 1,7 1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>

#include "basis.h"
#include "printsdlerror.h"


@@ 65,10 64,6 @@ init(int chunkSize)
		return printSDLError("SDL_mixer could not initialize (Mix_OpenAudio).");
	}

	if(TTF_Init() < 0) {
		return printSDLError("SDL_ttf could not initialize.");
	}

	return 1;
}



@@ 192,7 187,6 @@ Basis_Quit()
	Mix_CloseAudio();
	IMG_Quit();
	Mix_Quit();
	TTF_Quit();
	SDL_Quit();
}


M src/loader.c => src/loader.c +5 -71
@@ 1,7 1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>

#include "loader.h"
#include "printsdlerror.h"


@@ 24,16 23,6 @@ static SDL_Renderer *_renderer = NULL;
static char *basePath = NULL;
static SDL_Surface *missingSurface = NULL;

Image *
createEmptyImage(ImageType type, LoadType loadType)
{
	Image *image = (Image *)malloc(sizeof(Image));
	image->texture = NULL;
	image->type = type;
	image->loadType = loadType;
	return image;
}

Sound *
createEmptySound()
{


@@ 61,7 50,7 @@ drawSurfaceToImage(Image *image, SDL_Surface *surface)
int
loadImage(Image *image)
{
	char *fullPath = concat(basePath, image->data.filePath);
	char *fullPath = concat(basePath, image->filePath);
	SDL_Surface *loadedSurface = IMG_Load(fullPath);
	if(loadedSurface == NULL) {
		printSDLError("SDL could not load image %s.", fullPath);


@@ 76,26 65,6 @@ loadImage(Image *image)
}

int
loadTextImage(Image *image)
{
	TextLine line = image->data.line;
	SDL_Surface *textSurface;
	if(line.wrapLength <= 0) {
		textSurface = TTF_RenderUTF8_Blended(line.font, line.text, line.color);
	} else {
		textSurface = TTF_RenderUTF8_Blended_Wrapped(line.font, line.text, line.color, line.wrapLength);
	}
	if(textSurface == NULL) {
		printSDLError("SDL could not render text surface: %s.", line.text);
	} else {
		drawSurfaceToImage(image, textSurface);
		SDL_FreeSurface(textSurface);
	}

	return image->texture != NULL;
}

int
loadSound(Sound *sound)
{
	char *fullPath = concat(basePath, sound->path);


@@ 140,28 109,10 @@ Loader_Quit()
Image *
Loader_CreateImage(Image **ptr, LoadType loadType, char *filePath)
{
	Image *image = createEmptyImage(IMAGETYPE_FILE, loadType);
	image->data.filePath = filePath;
	*ptr = image;
	return image;
}

TextLine
Loader_CreateTextLine(TTF_Font *font, char *text, SDL_Color color, int wrapLength)
{
	TextLine line;
	line.font = font;
	line.text = text;
	line.color = color;
	line.wrapLength = wrapLength;
	return line;
}

Image *
Loader_CreateTextImage(Image **ptr, LoadType loadType, TextLine *line)
{
	Image *image = createEmptyImage(IMAGETYPE_TEXT, loadType);
	image->data.line = *line;
	Image *image = (Image *)malloc(sizeof(Image));
	image->texture = NULL;
	image->loadType = loadType;
	image->filePath = filePath;
	*ptr = image;
	return image;
}


@@ 173,9 124,6 @@ Loader_LoadImage(Image *image)
		return 1;
	}
	Loader_UnloadImage(image);
	if(image->type == IMAGETYPE_TEXT) {
		return loadTextImage(image);
	}
	return loadImage(image);
}



@@ 292,17 240,3 @@ Loader_LoadCursor(SDL_Cursor **ptr, char *path)
	*ptr = cursor;
	return cursor != NULL;
}

int
Loader_LoadFont(TTF_Font **ptr, char *path, int size)
{
	char *fullPath = concat(basePath, path);
	TTF_Font *font = TTF_OpenFont(fullPath, size);
	if(font == NULL) {
		printSDLError("SDL could not load font %s.", fullPath);
	}

	free(fullPath);
	*ptr = font;
	return font != NULL;
}

M src/loader.h => src/loader.h +1 -23
@@ 3,7 3,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>

/*
Copyright (c) 2013-2021 Devine Lu Linvega


@@ 39,29 38,11 @@ typedef enum {
	LOADTYPE_LAZIER
} LoadType;

typedef enum {
	IMAGETYPE_FILE,
	IMAGETYPE_TEXT
} ImageType;

typedef struct {
	TTF_Font *font;
	char *text;
	SDL_Color color;
	int wrapLength;
} TextLine;

typedef union {
	char *filePath;
	TextLine line;
} ImageData;

typedef struct {
	int w, h;
	char *filePath;
	SDL_Texture *texture;
	LoadType loadType;
	ImageType type;
	ImageData data;
} Image;

typedef struct {


@@ 74,8 55,6 @@ void Loader_Init(SDL_Renderer *renderer);
void Loader_Quit();

Image *Loader_CreateImage(Image **ptr, LoadType loadType, char *path);
TextLine Loader_CreateTextLine(TTF_Font *font, char *text, SDL_Color color, int wrapLength);
Image *Loader_CreateTextImage(Image **ptr, LoadType loadType, TextLine *line);
int Loader_LoadImage(Image *image);
void Loader_UnloadImage(Image *image);
void Loader_DestroyImage(Image *image);


@@ 87,4 66,3 @@ void Loader_DestroySound(Sound *sound);

int Loader_LoadText(char **ptr, char *path);
int Loader_LoadCursor(SDL_Cursor **ptr, char *path);
int Loader_LoadFont(TTF_Font **ptr, char *path, int size);

M src/playground.c => src/playground.c +0 -21
@@ 1,6 1,5 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>

#include "playground.h"
#include "basis.h"


@@ 90,30 89,22 @@ static char *sealPaths[NUM_SEALS] = {
	"graphics/interface/progress.entente.svg",
	"graphics/interface/save.svg"};
static Image *seals[NUM_SEALS];
static Image *testText = NULL;
static Sound *music = NULL;
static Sound *ambience = NULL;
static Sound *sound = NULL;
static SDL_Rect particleRect;
static SDL_Point particleCenter;
static SDL_Rect testTextRect;
static TTF_Font *dinFont = NULL;

static int
loadAssets()
{
	int success = 1;
	int i;
	TextLine textLine;
	SDL_Color white = {0xFF, 0xFF, 0xFF, 0xFF};
	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_LoadFont(&dinFont, "fonts/din1451alt.ttf", 144);
	textLine = Loader_CreateTextLine(dinFont, "HIVERSAIRES", white, -1);
	success &= Loader_LoadImage(Loader_CreateTextImage(&testText, LOADTYPE_LAZY, &textLine));
	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));


@@ 133,12 124,8 @@ quit()
		Loader_DestroyImage(seals[i]);
		seals[i] = NULL;
	}
	Loader_DestroyImage(testText);
	testText = NULL;
	SDL_FreeCursor(testCursor);
	testCursor = NULL;
	TTF_CloseFont(dinFont);
	dinFont = NULL;
	Loader_DestroySound(music);
	music = NULL;
	Loader_DestroySound(ambience);


@@ 158,8 145,6 @@ processWindowResize(int w, int h)
	state.windowW = w;
	state.windowH = h;
	Sim_Resize(state.windowW, state.windowH);
	testTextRect.x = (state.windowW - testTextRect.w) / 2;
	testTextRect.y = (state.windowH - testTextRect.h) / 2;
}

static void


@@ 216,7 201,6 @@ render(SDL_Renderer *renderer)
{
	int i;
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, testText->texture, NULL, &testTextRect);

	for(i = 0; i < NUM_SEALS; i++) {
		Particle *p = &state.particles[i];


@@ 275,11 259,6 @@ Playground_Run(int argc, char *argv[])
	particleRect.w = particleRadius * 2;
	particleRect.h = particleRadius * 2;

	testTextRect.x = (state.windowW - testText->w) / 2;
	testTextRect.y = (state.windowH - testText->h) / 2;
	testTextRect.w = testText->w;
	testTextRect.h = testText->h;

	Sim_Init(state.windowW, state.windowH, particleRadius, SIM_DT);
	if(!state.loaded) {
		Sim_Randomize(state.particles, NUM_SEALS);