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
53 changes: 11 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
cmake_minimum_required(VERSION 3.6)
cmake_minimum_required(VERSION 3.16)
project(Tilengine VERSION 2.16.0 LANGUAGES C)

project(Tilengine)
file(GLOB TILENGINE_SOURCES "src/*.c")
list(FILTER TILENGINE_SOURCES EXCLUDE REGEX "(Test|Window)\\.c$")

add_library(Tilengine STATIC ${TILENGINE_SOURCES})
add_library(Tilengine::Tilengine ALIAS Tilengine)

file(GLOB SOURCES
"src/*.c"
target_include_directories(Tilengine PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)


# main library
list(REMOVE_ITEM SOURCES "src/Test.c")
add_library(${PROJECT_NAME} ${SOURCES})

if (UNIX AND NOT APPLE)
set(CMAKE_C_FLAGS "-m64 -msse2")
elseif (UNIX AND APPLE)
target_link_libraries(${PROJECT_NAME} PRIVATE "framework")
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE "c")
target_link_libraries(${PROJECT_NAME} PRIVATE "z")
target_link_libraries(${PROJECT_NAME} PRIVATE "png")

find_package(SDL2 REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE ${SDL2_LIBRARIES})

target_include_directories(${PROJECT_NAME} PRIVATE include)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -O2 -fpic -DLIB_EXPORTS")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -Wno-format-truncation")

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/include/Tilengine.h
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

# test executable
add_executable(Test src/Test.c)
target_include_directories(Test PRIVATE include)
target_link_libraries(Test PRIVATE "Tilengine")
target_link_libraries(Test PRIVATE "c")
target_link_libraries(Test PRIVATE "z")
target_link_libraries(Test PRIVATE "png")
target_link_libraries(Test PRIVATE "m")

# samples
add_subdirectory(samples)
target_compile_definitions(Tilengine PRIVATE LIB_EXPORTS TLN_EXCLUDE_WINDOW)
target_compile_options(Tilengine PRIVATE -std=c99 -O2 -Wno-unused-result)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

Tilengine is an open source, cross-platform 2D graphics engine for creating classic/retro games with tile maps, sprites and palettes. Its unique scanline-based rendering algorithm makes raster effects a core feature, a technique used by many games running on real 2D graphics chips.

# This fork

This fork, in the macos branch,
- allows the use of a bundled stb_image instead of libpng via -DTLN_OPTION_LIBPNG=ON
- only uses SDL2 if the -DTLN_OPTION_SAMPLES=ON flag is set
- fixes macOS setting issues in the CMakeLists.txt

http://www.tilengine.org

# Contents
Expand Down
8 changes: 4 additions & 4 deletions samples/Actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* local variables */
static Actor* actors;
static int count = 0;
static unsigned int time;
static unsigned int actor_time;

/* local prototypes */
static void TasksActor (Actor* actor);
Expand Down Expand Up @@ -112,7 +112,7 @@ void TasksActors (unsigned int t)
if (!actors)
return;

time = t;
actor_time = t;

for (c=0; c<count; c++)
{
Expand All @@ -135,13 +135,13 @@ bool CheckActorCollision (Actor* actor1, Actor* actor2)
/* sets generic timeout */
void SetActorTimeout (Actor* actor, int timer, int timeout)
{
actor->timers[timer] = time + timeout;
actor->timers[timer] = actor_time + timeout;
}

/* gets generic timeout ended */
bool GetActorTimeout (Actor* actor, int timer)
{
return time >= actor->timers[timer];
return actor_time >= actor->timers[timer];
}

/* TasksActor */
Expand Down
7 changes: 6 additions & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ find_package(SDL2 REQUIRED)
link_libraries(${SDL2_LIBRARIES})

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -O2 -fpic -DLIB_EXPORTS")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -Wno-format-truncation")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result")

# gcc specific
if (UNIX AND NOT APPLE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format-truncation")
endif()

file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

Expand Down
10 changes: 5 additions & 5 deletions samples/Racer.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int pos;
int speed;
int last_tree;
unsigned int frame;
unsigned int time;
unsigned int racer_time;
int pan = 0;

static void raster_callback (int line);
Expand Down Expand Up @@ -81,7 +81,7 @@ int main (int argc, char* argv[])
while (TLN_ProcessWindow ())
{
/* timekeeper */
time = frame;
racer_time = frame;

TLN_SetLayerPosition (LAYER_PLAYFIELD, 56,72);
if (pos - last_tree >= 100)
Expand All @@ -92,7 +92,7 @@ int main (int argc, char* argv[])
}

/* input */
if ((time & 0x07) == 0)
if ((racer_time & 0x07) == 0)
{
if (TLN_GetInput (INPUT_UP) && speed < MAX_SPEED)
speed++;
Expand All @@ -107,10 +107,10 @@ int main (int argc, char* argv[])

/* actores */
pos += speed;
TasksActors (time);
TasksActors (racer_time);

/* render to window */
TLN_DrawFrame (time);
TLN_DrawFrame (racer_time);

frame++;
}
Expand Down
200 changes: 108 additions & 92 deletions samples/Shooter.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int pos_foreground[3] = {0};
int pos_background[3] = {0};
int inc_background[3] = {0};
unsigned int frame;
unsigned int time;
unsigned int racer_time;

uint8_t sky1[3] = {107,205,255};
uint8_t sky2[3] = {255,242,167};
Expand Down Expand Up @@ -97,112 +97,128 @@ static void FreeLayer (int index)
TLN_DeleteTilemap (layer->tilemap);
}

void ShooterInit(const char* resourceRoot)
{
int c;

/* setup engine */
TLN_Init (WIDTH,HEIGHT, MAX_LAYER, MAX_ACTOR, 0);
TLN_SetRasterCallback (raster_callback);
TLN_SetBGColor (136,238,204);

/* load resources*/
TLN_SetLoadPath (resourceRoot);
spritesets[SPRITESET_MAIN] = TLN_LoadSpriteset ("FireLeo");
spritesets[SPRITESET_HELLARM] = TLN_LoadSpriteset ("HellArm");
LoadLayer (LAYER_FOREGROUND, "TF4_fg1");
LoadLayer (LAYER_BACKGROUND, "TF4_bg1");

/* create sequences from sprite names */
sequences[SEQ_CLAW ] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "claw", 4);
sequences[SEQ_BLADE1] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "bladeb", 2);
sequences[SEQ_BLADE2] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "blades", 2);
sequences[SEQ_EXPLO1] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "explb", 3);
sequences[SEQ_EXPLO2] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "expls", 2);

/* create actors */
CreateActors (MAX_ACTOR);
CreateShip ();
BuildSinTable ();

/* compute increments for background scroll*/
inc_background[0] = float2fix(1.0f); /* 1.0 pixels/frame */
inc_background[1] = float2fix(1.2f); /* 1.2 pixels/frame*/
inc_background[2] = float2fix(8.0f); /* 8.0 pixels/frame*/

/* initial colors */
for (c=0; c<3; c++)
{
sky_hi[c] = sky1[c];
sky_lo[c] = sky2[c];
}
for (c=0; c<MAX_LAYER; c++)
palettes[c] = TLN_ClonePalette (TLN_GetTilesetPalette (layers[c].tileset));
}

void ShooterRun() {
/* timekeeper */
int c;

racer_time = frame;

/* bg color (sky) */
if (racer_time>=PAL_T0 && racer_time<=PAL_T1 && (racer_time&0x07)==0)
{
/* sky color */
for (c=0; c<3; c++)
{
sky_hi[c] = lerp(racer_time, PAL_T0,PAL_T1, sky1[c], sky3[c]);
sky_lo[c] = lerp(racer_time, PAL_T0,PAL_T1, sky2[c], sky4[c]);
}

for (c=0; c<MAX_LAYER; c++)
{
if (palettes[c])
TLN_DeletePalette (palettes[c]);
palettes[c] = TLN_ClonePalette (TLN_GetTilesetPalette (layers[c].tileset));
}
}

/* scroll */
for (c=0; c<3; c++)
pos_background[c] += inc_background[c];

/* layers */
TLN_SetLayer (LAYER_BACKGROUND, layers[LAYER_FOREGROUND].tileset, layers[LAYER_FOREGROUND].tilemap);
TLN_SetLayer (LAYER_FOREGROUND, layers[LAYER_BACKGROUND].tileset, layers[LAYER_BACKGROUND].tilemap);
TLN_SetLayerPosition (LAYER_BACKGROUND, racer_time/3, 160);
TLN_SetLayerPosition (LAYER_FOREGROUND, fix2int (pos_background[0]), 64);
TLN_SetLayerPalette (LAYER_FOREGROUND, palettes[LAYER_BACKGROUND]);
TLN_SetLayerPalette (LAYER_BACKGROUND, palettes[LAYER_FOREGROUND]);

if (racer_time < 500)
{
if (rand()%30 == 1)
CreateEnemy ();
}
else if (racer_time==600)
CreateBoss ();

/* actors */
TasksActors (racer_time);

frame++;
}

void ShooterRelease() {
/* deinit */
FreeLayer (LAYER_FOREGROUND);
FreeLayer (LAYER_BACKGROUND);
TLN_Deinit ();
}

#if TLN_HAVE_SDL2
/* entry point */
int main (int argc, char *argv[])
{
int c;

/* setup engine */
TLN_Init (WIDTH,HEIGHT, MAX_LAYER, MAX_ACTOR, 0);
TLN_SetRasterCallback (raster_callback);
TLN_SetBGColor (136,238,204);

/* load resources*/
TLN_SetLoadPath ("assets/tf4");
spritesets[SPRITESET_MAIN] = TLN_LoadSpriteset ("FireLeo");
spritesets[SPRITESET_HELLARM] = TLN_LoadSpriteset ("HellArm");
LoadLayer (LAYER_FOREGROUND, "TF4_fg1");
LoadLayer (LAYER_BACKGROUND, "TF4_bg1");

/* create sequences from sprite names */
sequences[SEQ_CLAW ] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "claw", 4);
sequences[SEQ_BLADE1] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "bladeb", 2);
sequences[SEQ_BLADE2] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "blades", 2);
sequences[SEQ_EXPLO1] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "explb", 3);
sequences[SEQ_EXPLO2] = TLN_CreateSpriteSequence (NULL, spritesets[SPRITESET_MAIN], "expls", 2);

/* create actors */
CreateActors (MAX_ACTOR);
CreateShip ();
BuildSinTable ();

/* compute increments for background scroll*/
inc_background[0] = float2fix(1.0f); /* 1.0 pixels/frame */
inc_background[1] = float2fix(1.2f); /* 1.2 pixels/frame*/
inc_background[2] = float2fix(8.0f); /* 8.0 pixels/frame*/

/* initial colors */
for (c=0; c<3; c++)
{
sky_hi[c] = sky1[c];
sky_lo[c] = sky2[c];
}
for (c=0; c<MAX_LAYER; c++)
palettes[c] = TLN_ClonePalette (TLN_GetTilesetPalette (layers[c].tileset));
ShooterInit("assets/tf4");

/* startup display */
TLN_CreateWindow (NULL, 0);

/* main loop */
while (TLN_ProcessWindow ())
{
/* timekeeper */
time = frame;

/* bg color (sky) */
if (time>=PAL_T0 && time<=PAL_T1 && (time&0x07)==0)
{
/* sky color */
for (c=0; c<3; c++)
{
sky_hi[c] = lerp(time, PAL_T0,PAL_T1, sky1[c], sky3[c]);
sky_lo[c] = lerp(time, PAL_T0,PAL_T1, sky2[c], sky4[c]);
}

for (c=0; c<MAX_LAYER; c++)
{
if (palettes[c])
TLN_DeletePalette (palettes[c]);
palettes[c] = TLN_ClonePalette (TLN_GetTilesetPalette (layers[c].tileset));
}
}

/* scroll */
for (c=0; c<3; c++)
pos_background[c] += inc_background[c];

/* layers */
TLN_SetLayer (LAYER_BACKGROUND, layers[LAYER_FOREGROUND].tileset, layers[LAYER_FOREGROUND].tilemap);
TLN_SetLayer (LAYER_FOREGROUND, layers[LAYER_BACKGROUND].tileset, layers[LAYER_BACKGROUND].tilemap);
TLN_SetLayerPosition (LAYER_BACKGROUND, time/3, 160);
TLN_SetLayerPosition (LAYER_FOREGROUND, fix2int (pos_background[0]), 64);
TLN_SetLayerPalette (LAYER_FOREGROUND, palettes[LAYER_BACKGROUND]);
TLN_SetLayerPalette (LAYER_BACKGROUND, palettes[LAYER_FOREGROUND]);

if (time < 500)
{
if (rand()%30 == 1)
CreateEnemy ();
}
else if (time==600)
CreateBoss ();

/* actors */
TasksActors (time);
ShooterRun();

/* render to window */
TLN_DrawFrame (time);

frame++;
TLN_DrawFrame (racer_time);
}

/* deinit */
FreeLayer (LAYER_FOREGROUND);
FreeLayer (LAYER_BACKGROUND);
TLN_Deinit ();

ShooterRelease();
return 0;
}
#endif

/* raster callback (virtual HBLANK) */
static void raster_callback (int line)
Expand All @@ -218,7 +234,7 @@ static void raster_callback (int line)

/* foreground */
if (line==32)
TLN_SetLayerPosition (LAYER_BACKGROUND, time/4, 160);
TLN_SetLayerPosition (LAYER_BACKGROUND, racer_time/4, 160);

if (line==64)
{
Expand Down
Loading