Advertisement
Guest User

abecadlo

a guest
Aug 29th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdint.h>
  3. #include <dsound.h>
  4. //Makes function or var invisible to all translation units but this one
  5. #define internal static
  6. #define global_variable static
  7. //Saves the local variable to the same value on next entry
  8. #define local_persist static
  9.  
  10. typedef uint8_t uint8;
  11. typedef uint16_t uint16;
  12. typedef uint32_t uint32;
  13. typedef uint64_t uint64;
  14.  
  15. typedef int8_t int8;
  16. typedef int16_t int16;
  17. typedef int32_t int32;
  18. typedef int64_t int64;
  19. struct offscreen_buffer
  20. {
  21. BITMAPINFO Info;
  22. void *Memory;
  23.  
  24. int Width;
  25. int Height;
  26. int Pitch;
  27. int BytesPerPixel;
  28. };
  29.  
  30. struct window_dimension
  31. {
  32. int Width;
  33. int Height;
  34. };
  35.  
  36. global_variable bool Running;
  37. global_variable offscreen_buffer GlobalBackBuffer;
  38. global_variable int XOffset = 0;
  39. global_variable int YOffset = 0;
  40. global_variable bool IsRed = 0;
  41.  
  42. #define DIRECT_SOUND_CREATE(name) HRESULT WINAPI name( LPCGUID pcGuidDevice, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter)
  43. typedef DIRECT_SOUND_CREATE(direct_sound_create);
  44. /*#define SET_FORMAT(name) HRESULT name( LPCWAVEFORMATEX lpcfxFormat )
  45. typedef SET_FORMAT(set_format);*/
  46.  
  47. internal void InitDSound( HWND Window, int32 SamplesPerSecond, int32 BufferSize )
  48. {
  49. // Load the library (if there's no sound, no reason to crash the game)
  50. HMODULE DSoundLibrary = LoadLibrary( "dsound.dll" );
  51. if( DSoundLibrary )
  52. {
  53. direct_sound_create *DirectSoundCreate = ( direct_sound_create* )GetProcAddress( DSoundLibrary, "DirectSoundCreate" );
  54. //set_format *SetFormat = ( set_format* )GetProcAddress( DSoundLibrary, "SetFormat" );
  55. LPDIRECTSOUND DirectSound;
  56. if( DirectSoundCreate && SUCCEEDED( DirectSoundCreate( 0, &DirectSound, 0 ) ) )
  57. {
  58. WAVEFORMATEX WaveFormat = {};
  59. WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
  60. WaveFormat.nChannels = 2;
  61. WaveFormat.nSamplesPerSec = SamplesPerSecond;
  62. WaveFormat.wBitsPerSample = 16;
  63. WaveFormat.nBlockAlign = ( WaveFormat.nChannels * WaveFormat.wBitsPerSample ) / 8;
  64. WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
  65. WaveFormat.cbSize = 0;
  66. if( SUCCEEDED( DirectSound -> SetCooperativeLevel( Window, DSSCL_PRIORITY ) ) )
  67. {
  68. DSBUFFERDESC BufferDescription = {};
  69. BufferDescription.dwSize = sizeof( BufferDescription );
  70. BufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
  71.  
  72. LPDIRECTSOUNDBUFFER PrimaryBuffer;
  73. if( SUCCEEDED( DirectSound -> CreateSoundBuffer( &BufferDescription, &PrimaryBuffer, 0 ) ) )
  74. {
  75. HRESULT Error = PrimaryBuffer->SetFormat( &WaveFormat );
  76. if( SUCCEEDED( Error ) )
  77. {
  78. OutputDebugString( " FORMAT WORKING FUCKYE ");
  79. }
  80. else
  81. {
  82. OutputDebugString( " Something went wrong ");
  83. }
  84. }
  85. else
  86. {
  87. OutputDebugString( " Something went wrong ");
  88. }
  89. }
  90. else
  91. {
  92. OutputDebugString( " Something went wrong ");
  93. }
  94.  
  95. LPDIRECTSOUNDBUFFER SecondaryBuffer;
  96. DSBUFFERDESC BufferDescription = { };
  97. BufferDescription.dwSize = sizeof( BufferDescription );
  98. BufferDescription.dwFlags = 0;
  99. BufferDescription.dwBufferBytes = BufferSize;
  100. BufferDescription.lpwfxFormat = &WaveFormat;
  101. if( SUCCEEDED( DirectSound -> CreateSoundBuffer( &BufferDescription, &SecondaryBuffer, 0 ) ) )
  102. {
  103. OutputDebugString( "Created secondarybuffer" );
  104. }
  105. else
  106. {
  107. OutputDebugString( "Something went wrong" );
  108. }
  109. }
  110. }
  111. }
  112.  
  113. window_dimension GetWindowDimension( HWND Window )
  114. {
  115. window_dimension Result;
  116.  
  117. RECT ClientRect;
  118. GetClientRect( Window, &ClientRect );
  119. Result.Width = ClientRect.right - ClientRect.left;
  120. Result.Height = ClientRect.bottom - ClientRect.top;
  121. return( Result );
  122. }
  123.  
  124. internal void RenderWeirdGradient( offscreen_buffer Buffer, int XOffset, int YOffset )
  125. {
  126. uint8 *Row = ( uint8 * ) Buffer.Memory;
  127. for( int Y = 0; Y < Buffer.Height; ++Y )
  128. {
  129. uint32 *Pixel = ( uint32* ) Row;
  130. for( int X = 0; X < Buffer.Width; ++X )
  131. {
  132. // Pixel colors in memory is:
  133. // 0xBBGGRRxx
  134.  
  135. uint8 Blue = ( X + XOffset );
  136. uint8 Green = ( Y + YOffset );
  137. uint8 Red = 0;
  138. if( IsRed )
  139. Red = 0 ;
  140. else
  141. Red = ( X + XOffset ) * ( Y + YOffset );
  142. *Pixel++ = ( (Red << 16 ) | ( Green << 8 ) | Blue );
  143. }
  144. Row += Buffer.Pitch;
  145. }
  146. }
  147. internal void ResizeDIBSection( offscreen_buffer *Buffer, int Width, int Height )
  148. {
  149. if( Buffer->Memory )
  150. {
  151. VirtualFree( Buffer->Memory, 0, MEM_RELEASE );
  152. }
  153.  
  154. Buffer->Width = Width;
  155. Buffer->Height = Height;
  156. Buffer->BytesPerPixel = 4;
  157.  
  158. //NOTE: biHeight is negative to tell windows to process bitmap
  159. //bottom-top except top-bottom
  160. Buffer->Info.bmiHeader.biSize = sizeof( Buffer->Info.bmiHeader );
  161. Buffer->Info.bmiHeader.biWidth = Buffer->Width;
  162. Buffer->Info.bmiHeader.biHeight = -Buffer->Height;
  163. Buffer->Info.bmiHeader.biPlanes = 1;
  164. Buffer->Info.bmiHeader.biCompression = BI_RGB;
  165. Buffer->Info.bmiHeader.biBitCount = 32;
  166.  
  167.  
  168. int BitmapMemorySize = Buffer->BytesPerPixel * Buffer->Width * Buffer->Height;
  169.  
  170. Buffer->Memory = VirtualAlloc( 0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE );
  171. Buffer->Pitch = Width * Buffer->BytesPerPixel;
  172. }
  173. internal void DisplayBuffer( HDC DeviceContext, int WindowWidth, int WindowHeight,
  174. offscreen_buffer Buffer, int X, int Y, int Width, int Height )
  175. {
  176.  
  177. StretchDIBits( DeviceContext,
  178. 0, 0, WindowWidth, WindowHeight,
  179. 0, 0, Buffer.Width, Buffer.Height,
  180. Buffer.Memory,
  181. &Buffer.Info,
  182. DIB_RGB_COLORS,
  183. SRCCOPY );
  184. }
  185.  
  186.  
  187. LRESULT CALLBACK MainWindowCallback( HWND Window,
  188. UINT Message,
  189. WPARAM WParam,
  190. LPARAM LParam )
  191. {
  192. LRESULT result = 0;
  193. switch( Message )
  194. {
  195. case WM_SIZE:
  196. {
  197.  
  198. }break;
  199. case WM_CLOSE:
  200. {
  201. Running = false;
  202. }break;
  203. case WM_ACTIVATEAPP:
  204. {
  205. //TODO: Zmiana aktywnosci (moze ma sie cos dziac)
  206. }break;
  207. case WM_DESTROY:
  208. {
  209. Running = false;
  210. }break;
  211. case WM_PAINT:
  212. {
  213. PAINTSTRUCT Paint;
  214. HDC DeviceContext = BeginPaint( Window, &Paint );
  215. int X = Paint.rcPaint.left;
  216. int Y = Paint.rcPaint.top;
  217. int Width = Paint.rcPaint.right - Paint.rcPaint.left;
  218. int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
  219.  
  220. window_dimension Dimension = GetWindowDimension( Window );
  221. DisplayBuffer( DeviceContext, Dimension.Width, Dimension.Height,
  222. GlobalBackBuffer, X, Y, Width, Height );
  223. EndPaint( Window, &Paint );
  224. }break;
  225. case WM_KEYDOWN:
  226. case WM_SYSKEYDOWN:
  227. {
  228. uint8 VKCode = WParam;
  229. bool WasDown = ( ( LParam & ( 1 << 30 ) ) != 0 );
  230. bool IsDown = ( ( LParam & ( 1 << 31 ) ) == 0 );
  231. //bool AltKeyWasDown = ( LParam & ( 1 << 29 ) ) != 0;
  232. if ( WasDown != IsDown )
  233. {
  234. switch( VKCode )
  235. {
  236. case VK_LEFT:
  237. XOffset = XOffset + 5;
  238. break;
  239. case VK_RIGHT:
  240. XOffset = XOffset - 5;
  241. break;
  242. case VK_UP:
  243. YOffset = YOffset + 5;
  244. break;
  245. case VK_DOWN:
  246. YOffset = YOffset - 5;
  247. break;
  248. case 0x52:
  249. IsRed = !IsRed;
  250. break;
  251. // TODO: Making alt+f4 working (set switch to if... else if... else if...
  252. /*case VK_F4 && AltKeyWasDown:
  253. Running = false;
  254. break;*/
  255. default:
  256. break;
  257.  
  258. }
  259. break;
  260. }
  261. }
  262. default:
  263. {
  264. result = DefWindowProc( Window, Message, WParam, LParam );
  265. }break;
  266. }
  267. return( result );
  268. }
  269. int CALLBACK WinMain( HINSTANCE Instance,
  270. HINSTANCE PrevInstance,
  271. LPSTR CommandLine,
  272. int ShowCode )
  273. {
  274. WNDCLASS WindowClass = { };
  275.  
  276. ResizeDIBSection( &GlobalBackBuffer, 1200, 720 );
  277.  
  278. WindowClass.style = CS_VREDRAW | CS_HREDRAW;
  279. WindowClass.lpfnWndProc = MainWindowCallback;
  280. WindowClass.hInstance = Instance;
  281. WindowClass.lpszClassName = "Handmade hero";
  282.  
  283. if( RegisterClass( &WindowClass ) )
  284. {
  285. HWND Window =
  286. CreateWindowEx( 0,
  287. WindowClass.lpszClassName,
  288. "Handmade hero",
  289. WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  290. CW_USEDEFAULT,
  291. CW_USEDEFAULT,
  292. CW_USEDEFAULT,
  293. CW_USEDEFAULT,
  294. 0,
  295. 0,
  296. Instance,
  297. 0 );
  298. if( Window )
  299. {
  300. HDC DeviceContext = GetDC( Window );
  301. Running = true;
  302.  
  303. InitDSound( Window, 48000, 48000 * sizeof(int16) * 2 );
  304. while( Running )
  305. {
  306. DeviceContext = GetDC( Window );
  307. MSG Message;
  308. while( PeekMessage( &Message, 0, 0, 0, PM_REMOVE ) )
  309. {
  310. if( Message.message == WM_QUIT )
  311. Running = false;
  312. TranslateMessage( &Message );
  313. DispatchMessage( &Message );
  314. }
  315.  
  316.  
  317. RenderWeirdGradient( GlobalBackBuffer, XOffset, YOffset );
  318.  
  319. window_dimension Dimension = GetWindowDimension( Window );
  320. DisplayBuffer( DeviceContext, Dimension.Width, Dimension.Height,
  321. GlobalBackBuffer, 0, 0, Dimension.Width, Dimension.Height );
  322. ReleaseDC( Window, DeviceContext );
  323. }
  324. }
  325. else
  326. {
  327. OutputDebugString( ( char* ) GetLastError( ) );
  328. //TODO: logging
  329. }
  330. }
  331. else
  332. {
  333. OutputDebugString( ( char* ) GetLastError( ) );
  334. //TODO: logging
  335. }
  336. return 0;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement