Skip to content

Commit

Permalink
Initial commitment for the basic rendering framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
weidaru committed Apr 13, 2013
1 parent d706367 commit 92e2a4d
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
water_simulation
================

Water rending with software render
Water rending with software render

Feel free to create your own project to the repository.
2 changes: 2 additions & 0 deletions water_rendering/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is the basic rendering framework using WIN32.
Do all the rending work in render(BackBuffer*) function, which is declared in render_wrapper.cpp.
204 changes: 204 additions & 0 deletions water_rendering/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "render_wrapper.h"

HWND winhwnd;
int window_width =800;
int window_height = 600;
struct BackBuffer back_buffer;
TCHAR * win_name = TEXT("Water") ;

// prototypes
LRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd );


inline void draw()
{
render(&back_buffer);

HDC winhdc = GetDC( winhwnd ) ;
// Now copy the back buffer to the front buffer.
BitBlt(

winhdc, // destination buffer
0, 0, // x, y to start writing to in the destination buffer
// counted from top left corner of dest HDC (window)

// Width, height to copy for
back_buffer.width, back_buffer.height,

back_buffer.back_dc, // SOURCE buffer. We're taking from that back canvas

0, 0, // x pt, y pt to START copying from
// SOURCE. counted from top left again

// And we just want a straight copy.
SRCCOPY );

ReleaseDC( winhwnd, winhdc ) ;
}




void InitBackBuffer()
{
back_buffer.width = window_width;
back_buffer.height = window_height;

HDC winhdc = GetDC( winhwnd );

/*
Imagine you have 2 things:
- a canvas on which to draw (call this your HBITMAP)
- set of brushes you will use to draw (call this your HDC)
1. HBITMAP. Canvas
___________________
| * |
| * |
| HBITMAP |
| draw here |
|_________________|
2. HDC. Set of brushes.
<-*
[]-*
()-*
*/

// Create the HBITMAP "canvas", or surface on which we will draw.
back_buffer.buffer_bmp = CreateCompatibleBitmap( winhdc, back_buffer.width, back_buffer.height );
if(back_buffer.buffer_bmp == NULL)
printf( "failed to create BackBufferBMP" );


// Create the HDC "device context", or collection of tools
// and brushes that we can use to draw on our canvas
back_buffer.back_dc = CreateCompatibleDC( winhdc );
if(back_buffer.back_dc == NULL)
printf( "failed to create the backDC" );

// Permanently associate the surface on which to draw
// ("canvas") (HBITMAP), with the "set of brushes" (HDC)

// "select in" the backBufferBMP into the backDC
HBITMAP oldbmp = (HBITMAP)SelectObject( back_buffer.back_dc, back_buffer.buffer_bmp );


// Delete the "canvas" that the backDC was
// going to draw to. We don't care about it.
DeleteObject( oldbmp );



/// Release the dc of the main window itself
ReleaseDC( winhwnd, winhdc );
}


// Just as said every C++ program starts at main, //
// every Windows program will start at WinMain. //
/////////////////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd )
{
#pragma region get window up
WNDCLASSEX window = { 0 } ;
window.cbSize = sizeof(WNDCLASSEX);
window.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); // BLACK_BRUSH, DKGRAY_BRUSH
window.hCursor = LoadCursor(NULL, IDC_ARROW); // load hand cursor.. make to guitar
window.hIcon = LoadIcon(NULL, IDI_APPLICATION); // large icon for app
window.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // small icon that shows up in top left of window
window.hInstance = hInstance; // program's instance handle that was first passed to WinMain by windows when program was first run
window.lpfnWndProc = WndProc; // function pointer to WndProc function
window.lpszClassName = win_name; // window class name
window.lpszMenuName = NULL; // menu name -- but our app has no menu now
window.style = CS_HREDRAW | CS_VREDRAW; // window style --

if(!RegisterClassEx( &window ))
{
MessageBox(NULL, TEXT("Something's wrong with the WNDCLASSEX structure you defined.. quitting"), TEXT("Error"), MB_OK);
return 1; // return from WinMain.. i.e. quit
}

// CREATE THE WINDOW AND KEEP THE HANDLE TO IT.
winhwnd = CreateWindowEx( 0/*WS_EX_TOPMOST*/, // extended window style.. this sets the window to being always on top
win_name, // window class name.. defined above
TEXT("Water"),// title bar of window
WS_OVERLAPPEDWINDOW,// window style
400, 200, // initial x, y start position of window
window_width, window_height, // initial width, height of window. Can also be CW_USEDEFAULT to let Windows choose these values
NULL, NULL, // parent window, window menu
hInstance, NULL); // program instance handle, creation params

// now we show and paint our window, so it appears
ShowWindow( winhwnd, nShowCmd ); // you have to ask that your Window be shown to see it
UpdateWindow(winhwnd); // paint the window
#pragma endregion

#pragma region message loop

MSG msg;

while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
PostQuitMessage(0);
break;
}
TranslateMessage(&msg); // translates 'character' keyboard messages
DispatchMessage(&msg); // send off to WndProc for processing
}
else
{
// could put more update code in here
// before drawing.
draw();
}
}
#pragma endregion

return 0; // end program once we exit the message loop
}




////////////////////////////////////////////////////////////////////////
// WndProc - "Window procedure" function
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
// As soon as the window is first created, create
// the back buffer.
InitBackBuffer() ;

return 0;
}
break;

case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}

// If message was NOT handled by us, we pass it off to
// the windows operating system to handle it.
return DefWindowProc(hwnd, message, wParam, lParam);
}
18 changes: 18 additions & 0 deletions water_rendering/render_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "render_wrapper.h"

#include <stdio.h>

void render(struct BackBuffer* bf)
{
// now just draw something into it for fun
char buf[300];
sprintf( buf, "HELLO WATER!" );
SetBkColor(bf->back_dc, RGB(0, 0, 0));
SetTextColor(bf->back_dc, RGB(255,255,255));
TextOutA( bf->back_dc, 20, 20, buf, strlen(buf) );

//Draw a simple rectangle using SetPixel.
for(int i = 50 ; i<=150; i++)
for(int j = 50; j<=150; j++)
SetPixel(bf->back_dc, i, j, RGB(255,0,0));
}
13 changes: 13 additions & 0 deletions water_rendering/render_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <windows.h>

struct BackBuffer
{
int width, height;
HDC back_dc;
HBITMAP buffer_bmp;
};

/*
* All the rendering related stuffs goes here
*/
void render(struct BackBuffer* bf);
20 changes: 20 additions & 0 deletions water_rendering/water_rendering.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "water_rendering", "water_rendering.vcxproj", "{D4C3C817-A565-4C3C-A75F-25B46A86CC42}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D4C3C817-A565-4C3C-A75F-25B46A86CC42}.Debug|Win32.ActiveCfg = Debug|Win32
{D4C3C817-A565-4C3C-A75F-25B46A86CC42}.Debug|Win32.Build.0 = Debug|Win32
{D4C3C817-A565-4C3C-A75F-25B46A86CC42}.Release|Win32.ActiveCfg = Release|Win32
{D4C3C817-A565-4C3C-A75F-25B46A86CC42}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added water_rendering/water_rendering.suo
Binary file not shown.
73 changes: 73 additions & 0 deletions water_rendering/water_rendering.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{D4C3C817-A565-4C3C-A75F-25B46A86CC42}</ProjectGuid>
<RootNamespace>water_rendering</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<CompileAs>Default</CompileAs>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="render_wrapper.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="render_wrapper.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
30 changes: 30 additions & 0 deletions water_rendering/water_rendering.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
<ClCompile Include="render_wrapper.cpp">
<Filter>Resource Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="render_wrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions water_rendering/water_rendering.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

0 comments on commit 92e2a4d

Please sign in to comment.