Skip to content

Commit

Permalink
Merge pull request #743 from Daft-Freak/voxel-asset
Browse files Browse the repository at this point in the history
Use asset packer for voxel map data
  • Loading branch information
Gadgetoid authored Jan 6, 2022
2 parents 6b935b9 + a9672b0 commit 27b9434
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 174,854 deletions.
1 change: 1 addition & 0 deletions examples/voxel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ if(32BLIT_PICO)
endif()

blit_executable (voxel voxel.cpp)
blit_assets_yaml(voxel assets.yml)
blit_metadata (voxel metadata.yml)
4 changes: 4 additions & 0 deletions examples/voxel/assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
assets.cpp:
assets/demo.map:
name: demo_map
type: raw/binary
File renamed without changes.
174,829 changes: 0 additions & 174,829 deletions examples/voxel/map.hpp

This file was deleted.

48 changes: 24 additions & 24 deletions examples/voxel/voxel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
32blit Voxel Terrain demo
by Jonathan Williamson (lowfatcode)
Expand All @@ -7,7 +7,7 @@
#include <cstring>

#include "32blit.hpp"
#include "map.hpp"
#include "assets.hpp"

using namespace blit;

Expand Down Expand Up @@ -37,7 +37,7 @@ void load_map() {
//close_file(terrain_file);
//terrain_file = open_file(std::to_string(terrain_index) + ".map");

// load palette data
// load palette data
//if(read_file(terrain_file, 0, 768, (char *)buffer) == 768) {
for(uint16_t i = 0; i < 256; i++) {
colour_map_palette[i] = Pen(demo_map[i * 3 + 0], demo_map[i * 3 + 1], demo_map[i * 3 + 2]);
Expand Down Expand Up @@ -68,15 +68,15 @@ void load_tile(int16_t x, int16_t y) {
if(free_tiles.size() > 0) {
tiles[x][y] = free_tiles.back();
free_tiles.pop_back();

memcpy((char *)tiles[x][y], demo_map + offset, tile_size);

//read_file(terrain_file, offset, tile_size, (char *)tiles[x][y]);
}
}

uint16_t get_sample(int16_t x, int16_t y) {
// work out the tile coordinates for this sample
// work out the tile coordinates for this sample
uint8_t tx = (x >> 5) & 0x1f;
uint8_t ty = (y >> 5) & 0x1f;

Expand All @@ -98,24 +98,24 @@ float deg2rad(float d) {
}

void draw_world(Vec3 position, float angle, float lean, float horizon, float near, float far) {
static int16_t height_buffer[160];
static int16_t height_buffer[160];

memset(visited, 0, 32 * 32);

// reset the height buffer to the bottom of the screen
for(auto i = 0; i < screen.bounds.w; i++) {
height_buffer[i] = screen.bounds.h;
}

// convert angle into radians
angle = deg2rad(angle);
float sina = sinf(angle);
float cosa = cosf(angle);

// starting z value
float z = near;
while(z < far) {

while(z < far) {

// calculate the left and right Points for the current sample span
Vec2 frustrum_left = Vec2(-cosa * z - sina * z, sina * z - cosa * z);
Expand All @@ -142,30 +142,30 @@ void draw_world(Vec3 position, float angle, float lean, float horizon, float nea

uint16_t sample = get_sample(sample_point.x, sample_point.y);
uint8_t colour_index = sample >> 8;
// convert the height map sample into a y coordinate on screen

// convert the height map sample into a y coordinate on screen
int16_t height = (position.z - (sample & 0xff)) * invz + float(horizon) + sample_lean;

// if the height is smaller (further up the screen) than our current height buffer
// value then we need to draw a new vertical strip
if(height < height_buffer[i]) {
if(height < height_buffer[i]) {
// fetch the colour for this strip from the colour map
Pen colour = colour_map_palette[colour_index];

// blend terrain colour with pre-multiplied fog colour
colour.r = ((colour.r * fog_blend) >> 8) + fog.r;
colour.g = ((colour.g * fog_blend) >> 8) + fog.g;
colour.b = ((colour.b * fog_blend) >> 8) + fog.b;

// draw the vertical strip
screen.pen = colour;
screen.v_span(Point(i, height), height_buffer[i] - height);
screen.v_span(Point(i, height), height_buffer[i] - height);

// update the height buffer to save that we've draw this far
// up the screen
height_buffer[i] = height;
}

// move to the next sampling coordinate
sample_point += sample_step;
sample_lean += lean_step;
Expand All @@ -192,8 +192,8 @@ void render(uint32_t time_ms) {
20.0f + pitch, // horizon position
3.0f, // near distance
300.0f // far distance
);
uint32_t ms_end = now();
);
uint32_t ms_end = now();

for(uint8_t y = 0; y < 32; y++) {
for(uint8_t x = 0; x < 32; x++) {
Expand Down Expand Up @@ -222,23 +222,23 @@ void render(uint32_t time_ms) {
screen.watermark();
screen.mask = nullptr;
screen.pen = Pen(255, 255, 255);

screen.text(std::to_string(ms_end - ms_start), minimal_font, Point(1, 230));
screen.pen = Pen(255, 0, 0);
for (int i = 0; i < uint16_t(ms_end - ms_start); i++) {
screen.pen = Pen(i * 5, 255 - (i * 5), 0);
screen.rectangle(Rect(i * 3 + 1, 237, 2, 2));
}
}
}

void update(uint32_t time_ms) {
static uint16_t tick = 0;

tick++;

// update angle of player based on joystick input
float old_angle = angle;
angle -= joystick.x * 2.0f;
angle -= joystick.x * 2.0f;

lean = (angle - old_angle) * 10.0f;

Expand All @@ -261,7 +261,7 @@ void update(uint32_t time_ms) {
position.x = position.x > 1023.0f ? 0.0f : position.x;
position.y = position.y > 1023.0f ? 0.0f : position.y;


// remove any unused tiles from the cache
for(uint8_t y = 0; y < 32; y++) {
for(uint8_t x = 0; x < 32; x++) {
Expand Down Expand Up @@ -289,4 +289,4 @@ void update(uint32_t time_ms) {
}
load_map();
}
}
}
20 changes: 19 additions & 1 deletion vs/examples/voxel/voxel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProjectDir)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand All @@ -96,6 +100,9 @@
<AdditionalDependencies>32blit.lib;32blit-sdl.lib;SDL2.lib;SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(OutputPath);$(SolutionDir)sdl\lib\$(PlatformTarget)\</AdditionalLibraryDirectories>
</Link>
<PreBuildEvent>
<Command>python -m ttblit pack --force --config $(ProjectDir)..\..\..\examples\voxel\assets.yml --output $(ProjectDir)</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
Expand All @@ -112,6 +119,9 @@
<AdditionalLibraryDirectories>$(OutputPath);$(SolutionDir)sdl\lib\$(PlatformTarget)\</AdditionalLibraryDirectories>
<AdditionalDependencies>32blit.lib;32blit-sdl.lib;SDL2.lib;SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PreBuildEvent>
<Command>python -m ttblit pack --force --config $(ProjectDir)..\..\..\examples\voxel\assets.yml --output $(ProjectDir)</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
Expand All @@ -132,6 +142,9 @@
<AdditionalLibraryDirectories>$(OutputPath);$(SolutionDir)sdl\lib\$(PlatformTarget)\</AdditionalLibraryDirectories>
<AdditionalDependencies>32blit.lib;32blit-sdl.lib;SDL2.lib;SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PreBuildEvent>
<Command>python -m ttblit pack --force --config $(ProjectDir)..\..\..\examples\voxel\assets.yml --output $(ProjectDir)</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
Expand All @@ -152,12 +165,17 @@
<AdditionalLibraryDirectories>$(OutputPath);$(SolutionDir)sdl\lib\$(PlatformTarget)\</AdditionalLibraryDirectories>
<AdditionalDependencies>32blit.lib;32blit-sdl.lib;SDL2.lib;SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PreBuildEvent>
<Command>python -m ttblit pack --force --config $(ProjectDir)..\..\..\examples\voxel\assets.yml --output $(ProjectDir)</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\..\examples\voxel\voxel.hpp" />
<ClInclude Include="assets.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\examples\voxel\voxel.cpp" />
<ClCompile Include="assets.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\32blit-sdl\32blit-sdl.vcxproj">
Expand All @@ -170,4 +188,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

0 comments on commit 27b9434

Please sign in to comment.