Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdint.h>
- #include <dsound.h>
- //Makes function or var invisible to all translation units but this one
- #define internal static
- #define global_variable static
- //Saves the local variable to the same value on next entry
- #define local_persist static
- typedef uint8_t uint8;
- typedef uint16_t uint16;
- typedef uint32_t uint32;
- typedef uint64_t uint64;
- typedef int8_t int8;
- typedef int16_t int16;
- typedef int32_t int32;
- typedef int64_t int64;
- struct offscreen_buffer
- {
- BITMAPINFO Info;
- void *Memory;
- int Width;
- int Height;
- int Pitch;
- int BytesPerPixel;
- };
- struct window_dimension
- {
- int Width;
- int Height;
- };
- global_variable bool Running;
- global_variable offscreen_buffer GlobalBackBuffer;
- global_variable int XOffset = 0;
- global_variable int YOffset = 0;
- global_variable bool IsRed = 0;
- #define DIRECT_SOUND_CREATE(name) HRESULT WINAPI name( LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter)
- typedef DIRECT_SOUND_CREATE(direct_sound_create);
- /*#define SET_FORMAT(name) HRESULT name( LPCWAVEFORMATEX lpcfxFormat )
- typedef SET_FORMAT(set_format);*/
- internal void InitDSound( HWND Window, int32 SamplesPerSecond, int32 BufferSize )
- {
- // Load the library (if there's no sound, no reason to crash the game)
- HMODULE DSoundLibrary = LoadLibrary( "dsound.dll" );
- if( DSoundLibrary )
- {
- direct_sound_create *DirectSoundCreate = ( direct_sound_create* )GetProcAddress( DSoundLibrary, "DirectSoundCreate" );
- //set_format *SetFormat = ( set_format* )GetProcAddress( DSoundLibrary, "SetFormat" );
- LPDIRECTSOUND DirectSound;
- if( DirectSoundCreate && SUCCEEDED( DirectSoundCreate( 0, &DirectSound, 0 ) ) )
- {
- WAVEFORMATEX WaveFormat = {};
- WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
- WaveFormat.nChannels = 2;
- WaveFormat.nSamplesPerSec = SamplesPerSecond;
- WaveFormat.wBitsPerSample = 16;
- WaveFormat.nBlockAlign = ( WaveFormat.nChannels * WaveFormat.wBitsPerSample ) / 8;
- WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
- WaveFormat.cbSize = 0;
- if( SUCCEEDED( DirectSound -> SetCooperativeLevel( Window, DSSCL_PRIORITY ) ) )
- {
- DSBUFFERDESC BufferDescription = {};
- BufferDescription.dwSize = sizeof( BufferDescription );
- BufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
- LPDIRECTSOUNDBUFFER PrimaryBuffer;
- if( SUCCEEDED( DirectSound -> CreateSoundBuffer( &BufferDescription, &PrimaryBuffer, 0 ) ) )
- {
- HRESULT Error = PrimaryBuffer->SetFormat( &WaveFormat );
- if( SUCCEEDED( Error ) )
- {
- OutputDebugString( " FORMAT WORKING FUCKYE ");
- }
- else
- {
- OutputDebugString( " Something went wrong ");
- }
- }
- else
- {
- OutputDebugString( " Something went wrong ");
- }
- }
- else
- {
- OutputDebugString( " Something went wrong ");
- }
- LPDIRECTSOUNDBUFFER SecondaryBuffer;
- DSBUFFERDESC BufferDescription = { };
- BufferDescription.dwSize = sizeof( BufferDescription );
- BufferDescription.dwFlags = 0;
- BufferDescription.dwBufferBytes = BufferSize;
- BufferDescription.lpwfxFormat = &WaveFormat;
- if( SUCCEEDED( DirectSound -> CreateSoundBuffer( &BufferDescription, &SecondaryBuffer, 0 ) ) )
- {
- OutputDebugString( "Created secondarybuffer" );
- }
- else
- {
- OutputDebugString( "Something went wrong" );
- }
- }
- }
- }
- window_dimension GetWindowDimension( HWND Window )
- {
- window_dimension Result;
- RECT ClientRect;
- GetClientRect( Window, &ClientRect );
- Result.Width = ClientRect.right - ClientRect.left;
- Result.Height = ClientRect.bottom - ClientRect.top;
- return( Result );
- }
- internal void RenderWeirdGradient( offscreen_buffer Buffer, int XOffset, int YOffset )
- {
- uint8 *Row = ( uint8 * ) Buffer.Memory;
- for( int Y = 0; Y < Buffer.Height; ++Y )
- {
- uint32 *Pixel = ( uint32* ) Row;
- for( int X = 0; X < Buffer.Width; ++X )
- {
- // Pixel colors in memory is:
- // 0xBBGGRRxx
- uint8 Blue = ( X + XOffset );
- uint8 Green = ( Y + YOffset );
- uint8 Red = 0;
- if( IsRed )
- Red = 0 ;
- else
- Red = ( X + XOffset ) * ( Y + YOffset );
- *Pixel++ = ( (Red << 16 ) | ( Green << 8 ) | Blue );
- }
- Row += Buffer.Pitch;
- }
- }
- internal void ResizeDIBSection( offscreen_buffer *Buffer, int Width, int Height )
- {
- if( Buffer->Memory )
- {
- VirtualFree( Buffer->Memory, 0, MEM_RELEASE );
- }
- Buffer->Width = Width;
- Buffer->Height = Height;
- Buffer->BytesPerPixel = 4;
- //NOTE: biHeight is negative to tell windows to process bitmap
- //bottom-top except top-bottom
- Buffer->Info.bmiHeader.biSize = sizeof( Buffer->Info.bmiHeader );
- Buffer->Info.bmiHeader.biWidth = Buffer->Width;
- Buffer->Info.bmiHeader.biHeight = -Buffer->Height;
- Buffer->Info.bmiHeader.biPlanes = 1;
- Buffer->Info.bmiHeader.biCompression = BI_RGB;
- Buffer->Info.bmiHeader.biBitCount = 32;
- int BitmapMemorySize = Buffer->BytesPerPixel * Buffer->Width * Buffer->Height;
- Buffer->Memory = VirtualAlloc( 0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE );
- Buffer->Pitch = Width * Buffer->BytesPerPixel;
- }
- internal void DisplayBuffer( HDC DeviceContext, int WindowWidth, int WindowHeight,
- offscreen_buffer Buffer, int X, int Y, int Width, int Height )
- {
- StretchDIBits( DeviceContext,
- 0, 0, WindowWidth, WindowHeight,
- 0, 0, Buffer.Width, Buffer.Height,
- Buffer.Memory,
- &Buffer.Info,
- DIB_RGB_COLORS,
- SRCCOPY );
- }
- LRESULT CALLBACK MainWindowCallback( HWND Window,
- UINT Message,
- WPARAM WParam,
- LPARAM LParam )
- {
- LRESULT result = 0;
- switch( Message )
- {
- case WM_SIZE:
- {
- }break;
- case WM_CLOSE:
- {
- Running = false;
- }break;
- case WM_ACTIVATEAPP:
- {
- //TODO: Zmiana aktywnosci (moze ma sie cos dziac)
- }break;
- case WM_DESTROY:
- {
- Running = false;
- }break;
- case WM_PAINT:
- {
- PAINTSTRUCT Paint;
- HDC DeviceContext = BeginPaint( Window, &Paint );
- int X = Paint.rcPaint.left;
- int Y = Paint.rcPaint.top;
- int Width = Paint.rcPaint.right - Paint.rcPaint.left;
- int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
- window_dimension Dimension = GetWindowDimension( Window );
- DisplayBuffer( DeviceContext, Dimension.Width, Dimension.Height,
- GlobalBackBuffer, X, Y, Width, Height );
- EndPaint( Window, &Paint );
- }break;
- case WM_KEYDOWN:
- case WM_SYSKEYDOWN:
- {
- uint8 VKCode = WParam;
- bool WasDown = ( ( LParam & ( 1 << 30 ) ) != 0 );
- bool IsDown = ( ( LParam & ( 1 << 31 ) ) == 0 );
- //bool AltKeyWasDown = ( LParam & ( 1 << 29 ) ) != 0;
- if ( WasDown != IsDown )
- {
- switch( VKCode )
- {
- case VK_LEFT:
- XOffset = XOffset + 5;
- break;
- case VK_RIGHT:
- XOffset = XOffset - 5;
- break;
- case VK_UP:
- YOffset = YOffset + 5;
- break;
- case VK_DOWN:
- YOffset = YOffset - 5;
- break;
- case 0x52:
- IsRed = !IsRed;
- break;
- // TODO: Making alt+f4 working (set switch to if... else if... else if...
- /*case VK_F4 && AltKeyWasDown:
- Running = false;
- break;*/
- default:
- break;
- }
- break;
- }
- }
- default:
- {
- result = DefWindowProc( Window, Message, WParam, LParam );
- }break;
- }
- return( result );
- }
- int CALLBACK WinMain( HINSTANCE Instance,
- HINSTANCE PrevInstance,
- LPSTR CommandLine,
- int ShowCode )
- {
- WNDCLASS WindowClass = { };
- ResizeDIBSection( &GlobalBackBuffer, 1200, 720 );
- WindowClass.style = CS_VREDRAW | CS_HREDRAW;
- WindowClass.lpfnWndProc = MainWindowCallback;
- WindowClass.hInstance = Instance;
- WindowClass.lpszClassName = "Handmade hero";
- if( RegisterClass( &WindowClass ) )
- {
- HWND Window =
- CreateWindowEx( 0,
- WindowClass.lpszClassName,
- "Handmade hero",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 0,
- 0,
- Instance,
- 0 );
- if( Window )
- {
- HDC DeviceContext = GetDC( Window );
- Running = true;
- InitDSound( Window, 48000, 48000 * sizeof(int16) * 2 );
- while( Running )
- {
- DeviceContext = GetDC( Window );
- MSG Message;
- while( PeekMessage( &Message, 0, 0, 0, PM_REMOVE ) )
- {
- if( Message.message == WM_QUIT )
- Running = false;
- TranslateMessage( &Message );
- DispatchMessage( &Message );
- }
- RenderWeirdGradient( GlobalBackBuffer, XOffset, YOffset );
- window_dimension Dimension = GetWindowDimension( Window );
- DisplayBuffer( DeviceContext, Dimension.Width, Dimension.Height,
- GlobalBackBuffer, 0, 0, Dimension.Width, Dimension.Height );
- ReleaseDC( Window, DeviceContext );
- }
- }
- else
- {
- OutputDebugString( ( char* ) GetLastError( ) );
- //TODO: logging
- }
- }
- else
- {
- OutputDebugString( ( char* ) GetLastError( ) );
- //TODO: logging
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement