Day 38 complete

This commit is contained in:
Edward R. Gonzalez 2023-10-21 02:09:44 -04:00
parent 29ed6dc1ad
commit 9bfdf8288c
5 changed files with 103 additions and 49 deletions

View File

@ -45,6 +45,8 @@
<Define>Build_Debug</Define> <Define>Build_Debug</Define>
<Define>Build_Development</Define> <Define>Build_Development</Define>
<Define>INTELLISENSE_DIRECTIVES=1</Define> <Define>INTELLISENSE_DIRECTIVES=1</Define>
<Define>_WIN64</Define>
<Define>_MSC_VER</Define>
</Defines> </Defines>
<ConfigProperties> <ConfigProperties>
<ConfigAndPlatform> <ConfigAndPlatform>

View File

@ -7,7 +7,7 @@
#include "handmade.hpp" #include "handmade.hpp"
#include "tile_map.cpp" #include "tile_map.cpp"
#include "random.cpp #include "random.cpp"
#endif #endif
NS_ENGINE_BEGIN NS_ENGINE_BEGIN
@ -265,31 +265,42 @@ void draw_bitmap( OffscreenBuffer* buffer
max_y = buffer_height; max_y = buffer_height;
// Start with the pixel on the top left corner of the rectangle // Start with the pixel on the top left corner of the rectangle
u8* row = rcast(u8*, buffer->memory ) u8* dst_row = rcast(u8*, buffer->memory )
+ min_x * buffer->bytes_per_pixel + min_x * buffer->bytes_per_pixel
+ min_y * buffer->pitch; + min_y * buffer->pitch;
u32* src_row = bitmap->pixels + bitmap->width * (bitmap->height - 1);
s32 bmp_y = bmp_start_y;
for ( s32 y = min_y; y < max_y; ++ y ) for ( s32 y = min_y; y < max_y; ++ y )
{ {
s32* pixel_32 = rcast(s32*, row); u32* dst = rcast(u32*, dst_row);
u32* src = src_row;
s32 bmp_x = bmp_start_x;
for ( s32 x = min_x; x < max_x; ++ x ) for ( s32 x = min_x; x < max_x; ++ x )
{ {
u32 color = bitmap->pixels[ bmp_y * bitmap->width + bmp_x ]; #define extract( pixel, shift ) (( *pixel >> shift ) & 0xFF)
f32 alpha = scast(f32, extract(src, 24)) / 255.f;
// TODO(Ed): This is a bad alpha check, fix it. f32 src_R = scast(f32, extract(src, 16));
// if ( (color >> 24) != 0 ) f32 src_G = scast(f32, extract(src, 8));
// { f32 src_B = scast(f32, extract(src, 0));
*pixel_32 = color;
// } f32 dst_R = scast(f32, extract(dst, 16));
++ pixel_32; f32 dst_G = scast(f32, extract(dst, 8));
++ bmp_x; f32 dst_B = scast(f32, extract(dst, 0));
#undef extract
f32 red = (1 - alpha) * dst_R + alpha * src_R;
f32 green = (1 - alpha) * dst_G + alpha * src_G;
f32 blue = (1 - alpha) * dst_B + alpha * src_B;
*dst = u32(red + 0.5f) << 16 | u32(green + 0.5f) << 8 | u32(blue + 0.5f) << 0;
++ dst;
++ src;
} }
row += buffer->pitch; dst_row += buffer->pitch;
-- bmp_y; src_row -= bitmap->width;
} }
} }
@ -321,8 +332,7 @@ Bitmap load_bmp( platform::ModuleAPI* platform_api, Str file_path )
} }
BitmapHeaderPacked* header = pcast(BitmapHeaderPacked*, file.data); BitmapHeaderPacked* header = pcast(BitmapHeaderPacked*, file.data);
// Note: Byte order is in little-endian AA BB GG RR (bottom up) (ABGR) assert( header->compression == 3 );
//
// TODO(Ed) : Do not directly assign this, allocate the pixels to somewhere in game or engine persistent. // TODO(Ed) : Do not directly assign this, allocate the pixels to somewhere in game or engine persistent.
result.pixels = rcast(u32*, rcast(Byte*, file.data) + header->bitmap_offset); result.pixels = rcast(u32*, rcast(Byte*, file.data) + header->bitmap_offset);
@ -330,8 +340,22 @@ Bitmap load_bmp( platform::ModuleAPI* platform_api, Str file_path )
result.height = header->height; result.height = header->height;
result.bits_per_pixel = header->bits_per_pixel; result.bits_per_pixel = header->bits_per_pixel;
u32 red_shift = 0;
u32 green_shift = 0;
u32 blue_shift = 0;
u32 alpha_shift = 0;
b32 red_found = bitscan_forward( & red_shift, header->red_mask );
b32 green_found = bitscan_forward( & green_shift, header->green_mask );
b32 blue_found = bitscan_forward( & blue_shift, header->blue_mask );
b32 alpha_found = bitscan_forward( & alpha_shift, ~(header->red_mask | header->green_mask | header->blue_mask) );
assert( red_found );
assert( green_found );
assert( blue_found );
assert( alpha_found );
u32* src = result.pixels; u32* src = result.pixels;
// Note: Do not use this generically, code is bad)
for ( s32 y = 0; y < header->width; ++ y ) for ( s32 y = 0; y < header->width; ++ y )
{ {
for ( s32 x = 0; x < header->height; ++ x ) for ( s32 x = 0; x < header->height; ++ x )
@ -345,9 +369,13 @@ Bitmap load_bmp( platform::ModuleAPI* platform_api, Str file_path )
}; };
Pixel* px = rcast(Pixel*, src); Pixel* px = rcast(Pixel*, src);
Pixel cpy = *px;
// *src = (u32(px->Alpha) << 0) | (u32(px->Red) << 24) | (u32(px->Green) << 16) | (u32(px->Blue) << 8); u32 alpha = (( *src >> alpha_shift ) & 0xFF) << 24;
*src = (*src >> 8) | (*src << 24); u32 red = (( *src >> red_shift ) & 0xFF) << 16;
u32 green = (( *src >> green_shift ) & 0xFF) << 8;
u32 blue = (( *src >> blue_shift ) & 0xFF) << 0;
*src = alpha | red | green | blue;
++ src; ++ src;
} }
} }

View File

@ -15,36 +15,30 @@
/* Platform OS */ /* Platform OS */
#if defined( _WIN32 ) || defined( _WIN64 ) #if defined( _WIN32 ) || defined( _WIN64 )
# ifndef SYSTEM_WINDOWS # ifndef System_Windows
# define SYSTEM_WINDOWS 1 # define System_Windows 1
# endif # endif
#elif defined( __APPLE__ ) && defined( __MACH__ ) #elif defined( __APPLE__ ) && defined( __MACH__ )
# ifndef SYSTEM_OSX # ifndef System_OSX
# define SYSTEM_OSX 1 # define System_OSX 1
# endif
# ifndef SYSTEM_MACOS
# define SYSTEM_MACOS 1
# endif
# include <TargetConditionals.h>
# if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
# ifndef SYSTEM_IOS
# define SYSTEM_IOS 1
# endif # endif
# ifndef System_MacOS
# define System_MacOS 1
# endif # endif
#elif defined( __unix__ ) #elif defined( __unix__ )
# ifndef SYSTEM_UNIX # ifndef System_Unix
# define GEN_SYSTEM_UNIX 1 # define System_Unix 1
# endif # endif
# if defined( ANDROID ) || defined( __ANDROID__ ) # if defined( ANDROID ) || defined( __ANDROID__ )
# ifndef SYSTEM_ANDROID # ifndef System_Android
# define SYSTEM_ANDROID 1 # define System_Android 1
# endif # endif
# ifndef SYSTEM_LINUX # ifndef SYSTEM_LINUX
# define SYSTEM_LINUX 1 # define System_Linux 1
# endif # endif
# elif defined( __linux__ ) # elif defined( __linux__ )
# ifndef SYSTEM_LINUX # ifndef System_Linux
# define SYSTEM_LINUX 1 # define System_linux 1
# endif # endif
# else # else
# error This UNIX operating system is not supported # error This UNIX operating system is not supported
@ -56,11 +50,11 @@
/* Platform compiler */ /* Platform compiler */
#if defined( _MSC_VER ) #if defined( _MSC_VER )
# define COMPILER_MSVC 1 # define Compiler_MSVC 1
#elif defined( __clang__ ) #elif defined( __clang__ )
# define COMPILER_CLANG 1 # define Compiler_Clang 1
#else #else
# error Unknown compiler # error "Unknown compiler"
#endif #endif
#if defined( __has_attribute ) #if defined( __has_attribute )
@ -76,7 +70,7 @@
# include <stdarg.h> # include <stdarg.h>
# include <stddef.h> # include <stddef.h>
# if defined( SYSTEM_WINDOWS ) # if defined( System_Windows )
# include <intrin.h> # include <intrin.h>
# endif # endif

View File

@ -2,6 +2,7 @@
#if INTELLISENSE_DIRECTIVES #if INTELLISENSE_DIRECTIVES
#include <math.h> #include <math.h>
#include "grime.hpp"
#endif #endif
// TODO(Ed) : Convert all of these to platform-efficient versions // TODO(Ed) : Convert all of these to platform-efficient versions
@ -40,14 +41,43 @@ f32 sine( f32 angle )
return result; return result;
} }
inline
f32 cosine( f32 angle ) f32 cosine( f32 angle )
{ {
f32 result = cosf( angle ); f32 result = cosf( angle );
return result; return result;
} }
inline
f32 arc_tangent( f32 Y, f32 X ) f32 arc_tangent( f32 Y, f32 X )
{ {
f32 result = atan2f( Y, X ); f32 result = atan2f( Y, X );
return result; return result;
} }
inline
b32 bitscan_forward( u32* index, u32 value )
{
b32 found = false;
// TODO(Ed) : This file can technically be generated with a metaprogram or via the preprocessor..
// We can keep The definitions for each of these based on the platform and
// Choose which one gets added to te project that way (keeps the runtime definition clean)
#if Compiler_MSVC
found = _BitScanForward( rcast(unsigned long*, index), value );
#elif Compiler_Clang
*index = __builtin_ctz( value );
found = index;
#else
for ( u32 test = 0; test < 32; ++ test )
{
if ( value & (1 << test ))
{
*index = test;
found = true;
break;
}
}
#endif
return found;
}

View File

@ -3,18 +3,18 @@
// Joyshock grime wrapper // Joyshock grime wrapper
// JoyShock does not provide a proper c-linkage definition for its structs, so we have to push this warning ignore. // JoyShock does not provide a proper c-linkage definition for its structs, so we have to push this warning ignore.
#ifdef COMPILER_CLANG #ifdef Compiler_Clang
# pragma clang diagnostic push # pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wreturn-type-c-linkage" # pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
#elif COMPILER_MSVC #elif Compiler_MSVC
# pragma warning( push ) # pragma warning( push )
# pragma warning( disable: 4820 ) # pragma warning( disable: 4820 )
#endif #endif
#include "dependencies/JoyShockLibrary/JoyShockLibrary.h" #include "dependencies/JoyShockLibrary/JoyShockLibrary.h"
#ifdef COMPILER_CLANG #ifdef Compiler_Clang
# pragma clang diagnostic pop # pragma clang diagnostic pop
#elif COMPILER_MSVC #elif Compiler_MSVC
# pragma warning( pop ) # pragma warning( pop )
#endif #endif