Browse Source

add example

master
blobt 5 years ago
parent
commit
3238841dc6
  1. 197
      example/lesson001-windows/lesson001-window.vcproj
  2. 76
      example/lesson001-windows/window.cpp
  3. 197
      example/lesson002-windows/lesson002-windows.vcproj
  4. 117
      example/lesson002-windows/window2.cpp
  5. 5930
      example/lesson003-point/CELLMath.hpp
  6. 17
      example/lesson003-point/Raster.cpp
  7. 60
      example/lesson003-point/Raster.h
  8. 209
      example/lesson003-point/lesson003-point.vcproj
  9. 118
      example/lesson003-point/lesson003.cpp
  10. 5930
      example/lesson004-point改进/CELLMath.hpp
  11. 54
      example/lesson004-point改进/Raster.cpp
  12. 31
      example/lesson004-point改进/Raster.h
  13. 209
      example/lesson004-point改进/lesson004-point改进.vcproj
  14. 118
      example/lesson004-point改进/lesson004.cpp
  15. 5930
      example/lesson005-line/CELLMath.hpp
  16. 86
      example/lesson005-line/Raster.cpp
  17. 33
      example/lesson005-line/Raster.h
  18. 209
      example/lesson005-line/lesson005-line.vcproj
  19. 117
      example/lesson005-line/lesson005.cpp
  20. 5930
      example/lesson006-line2/CELLMath.hpp
  21. 53
      example/lesson006-line2/Raster.cpp
  22. 134
      example/lesson006-line2/Raster.h
  23. 209
      example/lesson006-line2/lesson006-line2.vcproj
  24. 117
      example/lesson006-line2/lesson006.cpp
  25. 5930
      example/lesson007-line-color/CELLMath.hpp
  26. 118
      example/lesson007-line-color/Raster.cpp
  27. 33
      example/lesson007-line-color/Raster.h
  28. 209
      example/lesson007-line-color/lesson007-line-color.vcproj
  29. 117
      example/lesson007-line-color/lesson007.cpp
  30. 5930
      example/lesson008-lineStrip/CELLMath.hpp
  31. 116
      example/lesson008-lineStrip/Raster.cpp
  32. 94
      example/lesson008-lineStrip/Raster.h
  33. 209
      example/lesson008-lineStrip/lesson008-lineStrip.vcproj
  34. 137
      example/lesson008-lineStrip/lesson008.cpp
  35. 5930
      example/lesson009-贝塞尔/CELLMath.hpp
  36. 116
      example/lesson009-贝塞尔/Raster.cpp
  37. 94
      example/lesson009-贝塞尔/Raster.h
  38. 209
      example/lesson009-贝塞尔/lesson009-贝塞尔.vcproj
  39. 145
      example/lesson009-贝塞尔/lesson009.cpp
  40. 5930
      example/lesson010-rect/CELLMath.hpp
  41. 161
      example/lesson010-rect/Raster.cpp
  42. 67
      example/lesson010-rect/Raster.h
  43. 209
      example/lesson010-rect/lesson010-rect.vcproj
  44. 147
      example/lesson010-rect/lesson010.cpp
  45. 5930
      example/lesson011-rectEx/CELLMath.hpp
  46. 161
      example/lesson011-rectEx/Raster.cpp
  47. 73
      example/lesson011-rectEx/Raster.h
  48. 209
      example/lesson011-rectEx/lesson011-rectEx.vcproj
  49. 147
      example/lesson011-rectEx/lesson011.cpp
  50. 5930
      example/lesson012-rect-color/CELLMath.hpp
  51. 178
      example/lesson012-rect-color/Raster.cpp
  52. 82
      example/lesson012-rect-color/Raster.h
  53. 209
      example/lesson012-rect-color/lesson012-rect-color.vcproj
  54. 164
      example/lesson012-rect-color/lesson012.cpp
  55. 5930
      example/lesson013-triangle/CELLMath.hpp
  56. 202
      example/lesson013-triangle/Raster.cpp
  57. 69
      example/lesson013-triangle/Raster.h
  58. 209
      example/lesson013-triangle/lesson013-triangle.vcproj
  59. 164
      example/lesson013-triangle/lesson013.cpp
  60. 5930
      example/lesson014-span/CELLMath.hpp
  61. 202
      example/lesson014-span/Raster.cpp
  62. 149
      example/lesson014-span/Raster.h
  63. 209
      example/lesson014-span/lesson014-span.vcproj
  64. 164
      example/lesson014-span/lesson014.cpp
  65. 5930
      example/lesson015-span/CELLMath.hpp
  66. 202
      example/lesson015-span/Raster.cpp
  67. 183
      example/lesson015-span/Raster.h
  68. 209
      example/lesson015-span/lesson015-span.vcproj
  69. 122
      example/lesson015-span/lesson015.cpp
  70. 5930
      example/lesson016-triangle-color/CELLMath.hpp
  71. 202
      example/lesson016-triangle-color/Raster.cpp
  72. 204
      example/lesson016-triangle-color/Raster.h
  73. 209
      example/lesson016-triangle-color/lesson016-triangle-color.vcproj
  74. 126
      example/lesson016-triangle-color/lesson016.cpp
  75. 5938
      example/lesson017-优化/CELLMath.hpp
  76. 53
      example/lesson017-优化/CELLTimestamp.hpp
  77. 202
      example/lesson017-优化/Raster.cpp
  78. 208
      example/lesson017-优化/Raster.h
  79. 211
      example/lesson017-优化/lesson017-优化.vcproj
  80. 142
      example/lesson017-优化/lesson017.cpp
  81. 5938
      example/lesson018-优化2/CELLMath.hpp
  82. 53
      example/lesson018-优化2/CELLTimestamp.hpp
  83. 202
      example/lesson018-优化2/Raster.cpp
  84. 222
      example/lesson018-优化2/Raster.h
  85. 211
      example/lesson018-优化2/lesson018-优化2.vcproj
  86. 142
      example/lesson018-优化2/lesson018.cpp
  87. 5938
      example/lesson019-优化3/CELLMath.hpp
  88. 53
      example/lesson019-优化3/CELLTimestamp.hpp
  89. 202
      example/lesson019-优化3/Raster.cpp
  90. 229
      example/lesson019-优化3/Raster.h
  91. 211
      example/lesson019-优化3/lesson019-优化3.vcproj
  92. 142
      example/lesson019-优化3/lesson019.cpp
  93. 5938
      example/lesson020-优化4/CELLMath.hpp
  94. 53
      example/lesson020-优化4/CELLTimestamp.hpp
  95. 202
      example/lesson020-优化4/Raster.cpp
  96. 232
      example/lesson020-优化4/Raster.h
  97. 142
      example/lesson020-优化4/lesson019.cpp
  98. 211
      example/lesson020-优化4/lesson020-优化4.vcproj
  99. 5938
      example/lesson200-drawImage/CELLMath.hpp
  100. 53
      example/lesson200-drawImage/CELLTimestamp.hpp

197
example/lesson001-windows/lesson001-window.vcproj

@ -0,0 +1,197 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson001-window"
ProjectGUID="{A8549F81-52DC-490D-8D42-BF70939CF963}"
RootNamespace="window"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\window.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

76
example/lesson001-windows/window.cpp

@ -0,0 +1,76 @@
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowEx(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return 0;
}

197
example/lesson002-windows/lesson002-windows.vcproj

@ -0,0 +1,197 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson002-windows"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF001}"
RootNamespace="lesson002-windows"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\window2.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

117
example/lesson002-windows/window2.cpp

@ -0,0 +1,117 @@
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 注册窗口类
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 创建窗口
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
memset(buffer,0,width * height * 4);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
memset(buffer,0,width * height * 4);
//! 修改其中一部分
unsigned char* rgba = (unsigned char*)buffer;
int pitch = width * 4;
memset(rgba + pitch * 10,255, pitch);
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson003-point/CELLMath.hpp
File diff suppressed because it is too large
View File

17
example/lesson003-point/Raster.cpp

@ -0,0 +1,17 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster()
{
}
Raster::~Raster(void)
{
}
}

60
example/lesson003-point/Raster.h

@ -0,0 +1,60 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
class Raster
{
public:
Rgba _buffer[256][256];
public:
Raster();
~Raster(void);
void clear()
{
memset(_buffer,0,sizeof(_buffer));
}
void drawPoint(int x,int y, Rgba color,int ptSize)
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void setPixel(int x,int y,Rgba color)
{
if (x < 0 || y < 0 || x >= 256 || y >= 256)
{
return;
}
_buffer[y][x] = color;
}
};
}

209
example/lesson003-point/lesson003-point.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson003-point"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF003}"
RootNamespace="lesson003-point"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson003.cpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

118
example/lesson003-point/lesson003.cpp

@ -0,0 +1,118 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_POPUPWINDOW,
0,
0,
256,
256,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = 256;
bmpInfor.bmiHeader.biHeight = 256;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster;
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
for (int i = 0 ;i < 100 ; ++ i)
{
raster.drawPoint(rand()%256,rand()%256,CELL::Rgba(255,0,0),2);
}
memcpy(buffer,raster._buffer,sizeof(raster._buffer));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson004-point改进/CELLMath.hpp
File diff suppressed because it is too large
View File

54
example/lesson004-point改进/Raster.cpp

@ -0,0 +1,54 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
}

31
example/lesson004-point改进/Raster.h

@ -0,0 +1,31 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
public:
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson004-point改进/lesson004-point改进.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson004-point改进"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF004}"
RootNamespace="lesson004-point改进"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="源文件"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson004.cpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
</Filter>
<Filter
Name="头文件"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="资源文件"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

118
example/lesson004-point改进/lesson004.cpp

@ -0,0 +1,118 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_POPUPWINDOW,
0,
0,
256,
256,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
for (int i = 0 ;i < 100 ; ++ i)
{
raster.drawPoint(rand()%256,rand()%256,CELL::Rgba(255,0,0),2);
}
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson005-line/CELLMath.hpp
File diff suppressed because it is too large
View File

86
example/lesson005-line/Raster.cpp

@ -0,0 +1,86 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0)
{
float slope = yOffset / xOffset;
for (float y = pt1.y; y <= pt2.y ; y += 1.0f)
{
setPixel(pt2.x,y,color);
}
}
else if(yOffset == 0)
{
float slope = yOffset / xOffset;
for (float x = pt1.x; x <= pt2.x ; x += 1.0f)
{
setPixel(x,pt2.y,color);
}
}
else
{
float slope = yOffset / xOffset;
for (float x = pt1.x; x <= pt2.x ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
setPixel(x,y,color);
}
}
}
}

33
example/lesson005-line/Raster.h

@ -0,0 +1,33 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawLine(float2 pt1,float2 pt2,Rgba color);
public:
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson005-line/lesson005-line.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson005-line"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF005}"
RootNamespace="lesson005-line"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson005.cpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

117
example/lesson005-line/lesson005.cpp

@ -0,0 +1,117 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_POPUPWINDOW,
0,
0,
256,
256,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
raster.drawLine(CELL::float2(100,100),CELL::float2(200,200),CELL::Rgba(255,0,0));
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson006-line2/CELLMath.hpp
File diff suppressed because it is too large
View File

53
example/lesson006-line2/Raster.cpp

@ -0,0 +1,53 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
}

134
example/lesson006-line2/Raster.h

@ -0,0 +1,134 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawLine(float2 pt1,float2 pt2,Rgba color)
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color);
}
if (xOffset == 0)
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float slope = yOffset / xOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
setPixel(pt2.x,y,color);
}
}
else if(yOffset == 0)
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
setPixel(x,pt2.y,color);
}
}
else
{
if (fabs(xOffset) > fabs(yOffset))
{
float slope = yOffset / xOffset;
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
setPixel(x,y,color);
}
}
else
{
float slope = xOffset / yOffset;
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
for (float y = pt1.y; y <= pt2.y ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
setPixel(pt2.x,y,color);
}
}
}
}
public:
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson006-line2/lesson006-line2.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson006-line2"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF006}"
RootNamespace="lesson006-line2"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson006.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

117
example/lesson006-line2/lesson006.cpp

@ -0,0 +1,117 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_POPUPWINDOW,
0,
0,
256,
256,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
raster.drawLine(CELL::float2(-11,111),CELL::float2(666,222),CELL::Rgba(255,0,0));
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson007-line-color/CELLMath.hpp
File diff suppressed because it is too large
View File

118
example/lesson007-line-color/Raster.cpp

@ -0,0 +1,118 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float slope = yOffset / xOffset;
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float slope = xOffset / yOffset;
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
for (float y = pt1.y; y <= pt2.y ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(pt2.x,y,color);
}
}
}
}

33
example/lesson007-line-color/Raster.h

@ -0,0 +1,33 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
public:
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson007-line-color/lesson007-line-color.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson007-line-color"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF007}"
RootNamespace="lesson007-line-color"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson007.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

117
example/lesson007-line-color/lesson007.cpp

@ -0,0 +1,117 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_POPUPWINDOW,
0,
0,
256,
256,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
raster.drawLine(CELL::float2(1,100),CELL::float2(200,33),CELL::Rgba(255,0,0),CELL::Rgba(0,255,0));
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson008-lineStrip/CELLMath.hpp
File diff suppressed because it is too large
View File

116
example/lesson008-lineStrip/Raster.cpp

@ -0,0 +1,116 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
}

94
example/lesson008-lineStrip/Raster.h

@ -0,0 +1,94 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count)
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; ++ i)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
public:
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson008-lineStrip/lesson008-lineStrip.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson008-lineStrip"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF008}"
RootNamespace="lesson008-lineStrip"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson008.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

137
example/lesson008-lineStrip/lesson008.cpp

@ -0,0 +1,137 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
256,
256,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 arPoints[] =
{
CELL::float2(11,34),
CELL::float2(33,66),
CELL::float2(1,100),
CELL::float2(22,88),
CELL::float2(100,1),
};
raster.drawArrays(CELL::DM_LINE_STRIP,arPoints,sizeof(arPoints)/sizeof(arPoints[0]));
CELL::float2 center(100,100);
float radius = 80;
CELL::float2 arCircle[360];
for (int i = 0 ;i < 360 ; ++ i)
{
float rad = DEG2RAD(i);
arCircle[i].x = radius * cos(rad) + center.x;
arCircle[i].y = radius * sin(rad) + center.y;
}
raster.drawArrays(CELL::DM_LINE_STRIP,arCircle,sizeof(arCircle)/sizeof(arCircle[0]));
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson009-贝塞尔/CELLMath.hpp
File diff suppressed because it is too large
View File

116
example/lesson009-贝塞尔/Raster.cpp

@ -0,0 +1,116 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
}

94
example/lesson009-贝塞尔/Raster.h

@ -0,0 +1,94 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count)
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
public:
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson009-贝塞尔/lesson009-贝塞尔.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson009-贝塞尔"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF009}"
RootNamespace="lesson009-贝塞尔"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="源文件"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson009.cpp"
>
</File>
</Filter>
<Filter
Name="头文件"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="资源文件"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

145
example/lesson009-贝塞尔/lesson009.cpp

@ -0,0 +1,145 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 points[] =
{
CELL::float2(50,50),
CELL::float2(200,50),
CELL::float2(33,88),
CELL::float2(159,100),
};
CELL::float2 prev[2];
for (float t = 0 ;t < 1.0f ; t += 0.01f)
{
float x = points[0].x * pow(1-t,3)
+ 3 * points[1].x * t * pow(1-t,2)
+ 3 * points[2].x * t*t * (1-t)
+ points[3].x * t * t * t;
float y = points[0].y * pow(1-t,3)
+ 3 * points[1].y * t * pow(1-t,2)
+ 3 * points[2].y * t*t * (1-t)
+ points[3].y * t * t * t;
if (t == 0)
{
prev[0] = CELL::float2(x,y);
}
else
{
prev[1] = CELL::float2(x,y);
raster.drawArrays(CELL::DM_LINES,prev,2);
prev[0] = prev[1];
}
}
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson010-rect/CELLMath.hpp
File diff suppressed because it is too large
View File

161
example/lesson010-rect/Raster.cpp

@ -0,0 +1,161 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
}

67
example/lesson010-rect/Raster.h

@ -0,0 +1,67 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h)
{
for (int x = startX ; x < startX + w ; ++ x)
{
for (int y = startY ; y < startY + h ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
public:
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson010-rect/lesson010-rect.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson010-rect"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF010}"
RootNamespace="lesson010-rect"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson010.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

147
example/lesson010-rect/lesson010.cpp

@ -0,0 +1,147 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 points[] =
{
CELL::float2(50,50),
CELL::float2(200,50),
CELL::float2(33,88),
CELL::float2(159,100),
};
CELL::float2 prev[2];
for (float t = 0 ;t < 1.0f ; t += 0.01f)
{
float x = points[0].x * pow(1-t,3)
+ 3 * points[1].x * t * pow(1-t,2)
+ 3 * points[2].x * t*t * (1-t)
+ points[3].x * t * t * t;
float y = points[0].y * pow(1-t,3)
+ 3 * points[1].y * t * pow(1-t,2)
+ 3 * points[2].y * t*t * (1-t)
+ points[3].y * t * t * t;
if (t == 0)
{
prev[0] = CELL::float2(x,y);
}
else
{
prev[1] = CELL::float2(x,y);
raster.drawArrays(CELL::DM_LINES,prev,2);
prev[0] = prev[1];
}
}
raster.drawFilleRect(-10,-10,80,80);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson011-rectEx/CELLMath.hpp
File diff suppressed because it is too large
View File

161
example/lesson011-rectEx/Raster.cpp

@ -0,0 +1,161 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
}

73
example/lesson011-rectEx/Raster.h

@ -0,0 +1,73 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h)
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
public:
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson011-rectEx/lesson011-rectEx.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson011-rectEx"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF011}"
RootNamespace="lesson011-rectEx"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson011.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

147
example/lesson011-rectEx/lesson011.cpp

@ -0,0 +1,147 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 points[] =
{
CELL::float2(50,50),
CELL::float2(200,50),
CELL::float2(33,88),
CELL::float2(159,100),
};
CELL::float2 prev[2];
for (float t = 0 ;t < 1.0f ; t += 0.01f)
{
float x = points[0].x * pow(1-t,3)
+ 3 * points[1].x * t * pow(1-t,2)
+ 3 * points[2].x * t*t * (1-t)
+ points[3].x * t * t * t;
float y = points[0].y * pow(1-t,3)
+ 3 * points[1].y * t * pow(1-t,2)
+ 3 * points[2].y * t*t * (1-t)
+ points[3].y * t * t * t;
if (t == 0)
{
prev[0] = CELL::float2(x,y);
}
else
{
prev[1] = CELL::float2(x,y);
raster.drawArrays(CELL::DM_LINES,prev,2);
prev[0] = prev[1];
}
}
raster.drawFilleRect(300,260,200,200);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson012-rect-color/CELLMath.hpp
File diff suppressed because it is too large
View File

178
example/lesson012-rect-color/Raster.cpp

@ -0,0 +1,178 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
}

82
example/lesson012-rect-color/Raster.h

@ -0,0 +1,82 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors)
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
public:
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson012-rect-color/lesson012-rect-color.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson012-rect-color"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF012}"
RootNamespace="lesson012-rect-color"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson012.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

164
example/lesson012-rect-color/lesson012.cpp

@ -0,0 +1,164 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 points[] =
{
CELL::float2(50,50),
CELL::float2(200,50),
CELL::float2(33,88),
CELL::float2(159,100),
};
CELL::float2 prev[2];
for (float t = 0 ;t < 1.0f ; t += 0.01f)
{
float x = points[0].x * pow(1-t,3)
+ 3 * points[1].x * t * pow(1-t,2)
+ 3 * points[2].x * t*t * (1-t)
+ points[3].x * t * t * t;
float y = points[0].y * pow(1-t,3)
+ 3 * points[1].y * t * pow(1-t,2)
+ 3 * points[2].y * t*t * (1-t)
+ points[3].y * t * t * t;
if (t == 0)
{
prev[0] = CELL::float2(x,y);
}
else
{
prev[1] = CELL::float2(x,y);
raster.drawArrays(CELL::DM_LINES,prev,2);
prev[0] = prev[1];
}
}
CELL::int2 pt[] =
{
CELL::int2(10,10),
CELL::int2(110,10),
CELL::int2(110,110),
CELL::int2(10,110),
};
CELL::Rgba colors[] =
{
CELL::Rgba (255,0,0),
CELL::Rgba (0,255,0),
CELL::Rgba (0,0,255),
CELL::Rgba (255,255,255),
};
raster.drawRect(pt,colors);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson013-triangle/CELLMath.hpp
File diff suppressed because it is too large
View File

202
example/lesson013-triangle/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

69
example/lesson013-triangle/Raster.h

@ -0,0 +1,69 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
void drawLine(int startX,int endX, int y,Rgba color1,Rgba color2)
{
float length = tmax<int>(endX - startX,1);
for (int x = startX ; x <= endX ; ++ x)
{
Rgba color = colorLerp(color1,color2,( x - startX)/length);
setPixel(x,y,color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson013-triangle/lesson013-triangle.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson013-triangle"
ProjectGUID="{A8549F81-8888-490D-8D42-BF70939CF013}"
RootNamespace="lesson013-triangle"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson013.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

164
example/lesson013-triangle/lesson013.cpp

@ -0,0 +1,164 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 points[] =
{
CELL::float2(50,50),
CELL::float2(200,50),
CELL::float2(33,88),
CELL::float2(159,100),
};
CELL::float2 prev[2];
for (float t = 0 ;t < 1.0f ; t += 0.01f)
{
float x = points[0].x * pow(1-t,3)
+ 3 * points[1].x * t * pow(1-t,2)
+ 3 * points[2].x * t*t * (1-t)
+ points[3].x * t * t * t;
float y = points[0].y * pow(1-t,3)
+ 3 * points[1].y * t * pow(1-t,2)
+ 3 * points[2].y * t*t * (1-t)
+ points[3].y * t * t * t;
if (t == 0)
{
prev[0] = CELL::float2(x,y);
}
else
{
prev[1] = CELL::float2(x,y);
raster.drawArrays(CELL::DM_LINES,prev,2);
prev[0] = prev[1];
}
}
CELL::int2 pt[] =
{
CELL::int2(10,10),
CELL::int2(110,10),
CELL::int2(110,110),
CELL::int2(10,110),
};
CELL::Rgba colors[] =
{
CELL::Rgba (255,0,0),
CELL::Rgba (0,255,0),
CELL::Rgba (0,0,255),
CELL::Rgba (255,255,255),
};
raster.drawRect(pt,colors);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson014-span/CELLMath.hpp
File diff suppressed because it is too large
View File

202
example/lesson014-span/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

149
example/lesson014-span/Raster.h

@ -0,0 +1,149 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
int _y;
public:
Span(int xStart,int xEnd,int y)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
int _x2;
int _y2;
Ege(int x1,int y1,int x2,int y2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
}
else
{
_x1 = x2;
_y1 = y2;
_x2 = x1;
_y2 = y1;
}
}
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
void drawEge(const Ege& e1,const Ege& e2)
{
float xOffset = e2._x2 - e2._x1;
float yOffset = e2._y2 - e2._y1;
float scale = 0;
float step = 1.0f/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float yOffset1 = e1._y2 - e1._y1;
float scale1 = 0;
float step1 = 1.0f/yOffset1;
for (int y = e2._y1 ; y <= e2._y2 ; ++ y)
{
int x1 = e1._x1 + scale1 * xOffset1;
int x2 = e2._x1 + scale * xOffset;
Span span(x1,x2,y);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
for (int x = span._xStart ; x <= span._xEnd; ++ x)
{
setPixel(x,span._y,_color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson014-span/lesson014-span.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson014-triangle-span"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C014}"
RootNamespace="lesson014-triangle-span"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson014.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

164
example/lesson014-span/lesson014.cpp

@ -0,0 +1,164 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::float2 points[] =
{
CELL::float2(50,50),
CELL::float2(200,50),
CELL::float2(33,88),
CELL::float2(159,100),
};
CELL::float2 prev[2];
for (float t = 0 ;t < 1.0f ; t += 0.01f)
{
float x = points[0].x * pow(1-t,3)
+ 3 * points[1].x * t * pow(1-t,2)
+ 3 * points[2].x * t*t * (1-t)
+ points[3].x * t * t * t;
float y = points[0].y * pow(1-t,3)
+ 3 * points[1].y * t * pow(1-t,2)
+ 3 * points[2].y * t*t * (1-t)
+ points[3].y * t * t * t;
if (t == 0)
{
prev[0] = CELL::float2(x,y);
}
else
{
prev[1] = CELL::float2(x,y);
raster.drawArrays(CELL::DM_LINES,prev,2);
prev[0] = prev[1];
}
}
CELL::int2 pt[] =
{
CELL::int2(10,10),
CELL::int2(110,10),
CELL::int2(110,110),
CELL::int2(10,110),
};
CELL::Rgba colors[] =
{
CELL::Rgba (255,0,0),
CELL::Rgba (0,255,0),
CELL::Rgba (0,0,255),
CELL::Rgba (255,255,255),
};
raster.drawRect(pt,colors);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson015-span/CELLMath.hpp
File diff suppressed because it is too large
View File

202
example/lesson015-span/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

183
example/lesson015-span/Raster.h

@ -0,0 +1,183 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
int _y;
public:
Span(int xStart,int xEnd,int y)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
int _x2;
int _y2;
Ege(int x1,int y1,int x2,int y2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_x2 = x2;
_y2 = y2;
}
else
{
_x1 = x2;
_y1 = y2;
_x2 = x1;
_y2 = y1;
}
}
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
void drawTriangle(int2 p0,int2 p1,int2 p2)
{
Ege eges[3] =
{
Ege(p0.x,p0.y,p1.x,p1.y),
Ege(p1.x,p1.y,p2.x,p2.y),
Ege(p2.x,p2.y,p0.x,p0.y),
};
int iMax = 0;
int length = eges[0]._y2 - eges[0]._y1;
for (int i = 1 ;i < 3 ; ++ i)
{
int len = eges[i]._y2 - eges[i]._y1;
if (len > length)
{
length = i;
}
}
int iShort1 = (iMax + 1)%3;
int iShort2 = (iMax + 2)%3;
drawEge(eges[iMax],eges[iShort1]);
drawEge(eges[iMax],eges[iShort2]);
}
void drawEge(const Ege& e1,const Ege& e2)
{
float yOffset1 = e1._y2 - e1._y1;
if (yOffset1 == 0)
{
return;
}
float yOffset = e2._y2 - e2._y1;
if (yOffset == 0)
{
return;
}
float xOffset = e2._x2 - e2._x1;
float scale = 0;
float step = 1.0f/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
float step1 = 1.0f/yOffset1;
for (int y = e2._y1 ; y < e2._y2 ; ++ y)
{
int x1 = e1._x1 + (int)(scale1 * xOffset1);
int x2 = e2._x1 + (int)(scale * xOffset);
Span span(x1,x2,y);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
for (int x = span._xStart ; x < span._xEnd; ++ x)
{
setPixel(x,span._y,_color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson015-span/lesson015-span.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson015-triangle-span"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C015}"
RootNamespace="lesson015-triangle-span"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson015.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

122
example/lesson015-span/lesson015.cpp

@ -0,0 +1,122 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::int2 pt[3] =
{
CELL::int2(100,10),
CELL::int2(10,100),
CELL::int2(200,100),
};
raster.drawTriangle(pt[0],pt[1],pt[2]);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5930
example/lesson016-triangle-color/CELLMath.hpp
File diff suppressed because it is too large
View File

202
example/lesson016-triangle-color/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (Rgba*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

204
example/lesson016-triangle-color/Raster.h

@ -0,0 +1,204 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
Rgba _colorStart;
Rgba _colorEnd;
int _y;
public:
Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_colorStart = colorStart;
_colorEnd = colorEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_colorStart = colorEnd;
_colorEnd = colorStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
Rgba _color1;
int _x2;
int _y2;
Rgba _color2;
Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_color1 = color1;
_x2 = x2;
_y2 = y2;
_color2 = color2;
}
else
{
_x1 = x2;
_y1 = y2;
_color1 = color2;
_x2 = x1;
_y2 = y1;
_color2 = color1;
}
}
};
class Raster
{
public:
Rgba* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
{
Ege eges[3] =
{
Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
};
int iMax = 0;
int length = eges[0]._y2 - eges[0]._y1;
for (int i = 1 ;i < 3 ; ++ i)
{
int len = eges[i]._y2 - eges[i]._y1;
if (len > length)
{
length = i;
}
}
int iShort1 = (iMax + 1)%3;
int iShort2 = (iMax + 2)%3;
drawEge(eges[iMax],eges[iShort1]);
drawEge(eges[iMax],eges[iShort2]);
}
void drawEge(const Ege& e1,const Ege& e2)
{
float yOffset1 = e1._y2 - e1._y1;
if (yOffset1 == 0)
{
return;
}
float yOffset = e2._y2 - e2._y1;
if (yOffset == 0)
{
return;
}
float xOffset = e2._x2 - e2._x1;
float scale = 0;
float step = 1.0f/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
float step1 = 1.0f/yOffset1;
for (int y = e2._y1 ; y < e2._y2 ; ++ y)
{
int x1 = e1._x1 + (int)(scale1 * xOffset1);
int x2 = e2._x1 + (int)(scale * xOffset);
Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
Span span(x1,x2,y,color1,color2);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
float length = span._xEnd - span._xStart;
for (int x = span._xStart ; x < span._xEnd; ++ x)
{
Rgba color = colorLerp(
span._colorStart
,span._colorEnd
,(float)(x - span._xStart)/length
);
setPixel(x,span._y,color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color;
}
};
}

209
example/lesson016-triangle-color/lesson016-triangle-color.vcproj

@ -0,0 +1,209 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson016-triangle-color"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C016}"
RootNamespace="lesson016-triangle-color"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Ô´Îļþ"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson016.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="×ÊÔ´Îļþ"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

126
example/lesson016-triangle-color/lesson016.cpp

@ -0,0 +1,126 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::int2 pt[3] =
{
CELL::int2(100,10),
CELL::int2(10,100),
CELL::int2(200,100),
};
CELL::Rgba color1(255,0,0);
CELL::Rgba color2(0,255,0);
CELL::Rgba color3(0,0,255);
raster.drawTriangle(pt[0],pt[1],pt[2],color1,color2,color3);
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5938
example/lesson017-优化/CELLMath.hpp
File diff suppressed because it is too large
View File

53
example/lesson017-优化/CELLTimestamp.hpp

@ -0,0 +1,53 @@
#pragma once
#include <windows.h>
namespace CELL
{
class CELLTimestamp
{
public:
CELLTimestamp()
{
QueryPerformanceFrequency(&_frequency);
QueryPerformanceCounter(&_startCount);
}
~CELLTimestamp()
{}
void update()
{
QueryPerformanceCounter(&_startCount);
}
/**
* »ñÈ¡µ±Ç°Ãë
*/
double getElapsedSecond()
{
return getElapsedTimeInMicroSec() * 0.000001;
}
/**
* »ñÈ¡ºÁÃë
*/
double getElapsedTimeInMilliSec()
{
return this->getElapsedTimeInMicroSec() * 0.001;
}
/**
* »ñȡ΢Ãî
*/
double getElapsedTimeInMicroSec()
{
LARGE_INTEGER endCount;
QueryPerformanceCounter(&endCount);
double startTimeInMicroSec = _startCount.QuadPart * (1000000.0 / _frequency.QuadPart);
double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / _frequency.QuadPart);
return endTimeInMicroSec - startTimeInMicroSec;
}
protected:
LARGE_INTEGER _frequency;
LARGE_INTEGER _startCount;
};
}

202
example/lesson017-优化/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (uint*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

208
example/lesson017-优化/Raster.h

@ -0,0 +1,208 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
Rgba _colorStart;
Rgba _colorEnd;
int _y;
public:
Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_colorStart = colorStart;
_colorEnd = colorEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_colorStart = colorEnd;
_colorEnd = colorStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
Rgba _color1;
int _x2;
int _y2;
Rgba _color2;
Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_color1 = color1;
_x2 = x2;
_y2 = y2;
_color2 = color2;
}
else
{
_x1 = x2;
_y1 = y2;
_color1 = color2;
_x2 = x1;
_y2 = y1;
_color2 = color1;
}
}
};
class Raster
{
public:
uint* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
{
Ege eges[3] =
{
Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
};
int iMax = 0;
int length = eges[0]._y2 - eges[0]._y1;
for (int i = 1 ;i < 3 ; ++ i)
{
int len = eges[i]._y2 - eges[i]._y1;
if (len > length)
{
length = i;
}
}
int iShort1 = (iMax + 1)%3;
int iShort2 = (iMax + 2)%3;
drawEge(eges[iMax],eges[iShort1]);
drawEge(eges[iMax],eges[iShort2]);
}
void drawEge(const Ege& e1,const Ege& e2)
{
float yOffset1 = e1._y2 - e1._y1;
if (yOffset1 == 0)
{
return;
}
float yOffset = e2._y2 - e2._y1;
if (yOffset == 0)
{
return;
}
float xOffset = e2._x2 - e2._x1;
float scale = 0;
float step = 1.0f/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
float step1 = 1.0f/yOffset1;
for (int y = e2._y1 ; y < e2._y2 ; ++ y)
{
int x1 = e1._x1 + (int)(scale1 * xOffset1);
int x2 = e2._x1 + (int)(scale * xOffset);
Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
Span span(x1,x2,y,color1,color2);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
float length = span._xEnd - span._xStart;
float scale = 0;
float step = 1.0f/length;
for (int x = span._xStart ; x < span._xEnd; ++ x)
{
Rgba color = colorLerp(
span._colorStart
,span._colorEnd
,scale
);
scale += step;
setPixel(x,span._y,color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color._color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color._color;
}
};
}

211
example/lesson017-优化/lesson017-优化.vcproj

@ -0,0 +1,211 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson017-优化"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C017}"
RootNamespace="lesson017-优化"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
EnableEnhancedInstructionSet="0"
FloatingPointModel="0"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="源文件"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson017.cpp"
>
</File>
</Filter>
<Filter
Name="头文件"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="资源文件"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

142
example/lesson017-优化/lesson017.cpp

@ -0,0 +1,142 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
#include "CELLTimestamp.hpp"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::int2 pt[3] =
{
CELL::int2(100,10),
CELL::int2(10,100),
CELL::int2(200,100),
};
CELL::Rgba color1(255,0,0);
CELL::Rgba color2(0,255,0);
CELL::Rgba color3(0,0,255);
CELL::CELLTimestamp tms;
tms.update();
raster.drawTriangle(pt[0],pt[1],pt[2],color1,color2,color3);
double mis = tms.getElapsedTimeInMicroSec();
char szBuf[128];
sprintf(szBuf,"%f ",mis);
int i = 00;
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
TextOut(hMem,10,10,szBuf,strlen(szBuf));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5938
example/lesson018-优化2/CELLMath.hpp
File diff suppressed because it is too large
View File

53
example/lesson018-优化2/CELLTimestamp.hpp

@ -0,0 +1,53 @@
#pragma once
#include <windows.h>
namespace CELL
{
class CELLTimestamp
{
public:
CELLTimestamp()
{
QueryPerformanceFrequency(&_frequency);
QueryPerformanceCounter(&_startCount);
}
~CELLTimestamp()
{}
void update()
{
QueryPerformanceCounter(&_startCount);
}
/**
* »ñÈ¡µ±Ç°Ãë
*/
double getElapsedSecond()
{
return getElapsedTimeInMicroSec() * 0.000001;
}
/**
* »ñÈ¡ºÁÃë
*/
double getElapsedTimeInMilliSec()
{
return this->getElapsedTimeInMicroSec() * 0.001;
}
/**
* »ñȡ΢Ãî
*/
double getElapsedTimeInMicroSec()
{
LARGE_INTEGER endCount;
QueryPerformanceCounter(&endCount);
double startTimeInMicroSec = _startCount.QuadPart * (1000000.0 / _frequency.QuadPart);
double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / _frequency.QuadPart);
return endTimeInMicroSec - startTimeInMicroSec;
}
protected:
LARGE_INTEGER _frequency;
LARGE_INTEGER _startCount;
};
}

202
example/lesson018-优化2/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (uint*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

222
example/lesson018-优化2/Raster.h

@ -0,0 +1,222 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
Rgba _colorStart;
Rgba _colorEnd;
int _y;
public:
Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_colorStart = colorStart;
_colorEnd = colorEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_colorStart = colorEnd;
_colorEnd = colorStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
Rgba _color1;
int _x2;
int _y2;
Rgba _color2;
Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_color1 = color1;
_x2 = x2;
_y2 = y2;
_color2 = color2;
}
else
{
_x1 = x2;
_y1 = y2;
_color1 = color2;
_x2 = x1;
_y2 = y1;
_color2 = color1;
}
}
};
class Raster
{
public:
uint* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
bool isInRect(int2 pt)
{
if (pt.x >= 0 && pt.x <= _width && pt.y >= 0 && pt.y <= _height)
{
return true;
}
return false;
}
void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
{
// if ((!isInRect(p0)) && (!isInRect(p1)) && (!isInRect(p2)))
// {
// return;
// }
Ege eges[3] =
{
Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
};
int iMax = 0;
int length = eges[0]._y2 - eges[0]._y1;
for (int i = 1 ;i < 3 ; ++ i)
{
int len = eges[i]._y2 - eges[i]._y1;
if (len > length)
{
length = len;
iMax = i;
}
}
int iShort1 = (iMax + 1)%3;
int iShort2 = (iMax + 2)%3;
drawEge(eges[iMax],eges[iShort1]);
drawEge(eges[iMax],eges[iShort2]);
}
void drawEge(const Ege& e1,const Ege& e2)
{
float yOffset1 = e1._y2 - e1._y1;
if (yOffset1 == 0)
{
return;
}
float yOffset = e2._y2 - e2._y1;
if (yOffset == 0)
{
return;
}
float xOffset = e2._x2 - e2._x1;
float scale = 0;
float step = 1.0f/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
float step1 = 1.0f/yOffset1;
for (int y = e2._y1 ; y < e2._y2 ; ++ y)
{
int x1 = e1._x1 + (int)(scale1 * xOffset1);
int x2 = e2._x1 + (int)(scale * xOffset);
Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
Span span(x1,x2,y,color1,color2);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
float length = span._xEnd - span._xStart;
float scale = 0;
float step = 1.0f/length;
for (int x = span._xStart ; x < span._xEnd; ++ x)
{
Rgba color = colorLerp(
span._colorStart
,span._colorEnd
,scale
);
scale += step;
setPixel(x,span._y,color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color._color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color._color;
}
};
}

211
example/lesson018-优化2/lesson018-优化2.vcproj

@ -0,0 +1,211 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson018-优化2"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C018}"
RootNamespace="lesson018-优化2"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
EnableEnhancedInstructionSet="0"
FloatingPointModel="0"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="源文件"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson018.cpp"
>
</File>
</Filter>
<Filter
Name="头文件"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="资源文件"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

142
example/lesson018-优化2/lesson018.cpp

@ -0,0 +1,142 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
#include "CELLTimestamp.hpp"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::int2 pt[3] =
{
CELL::int2(-100,-10),
CELL::int2(-10,-100),
CELL::int2(-200,-100),
};
CELL::Rgba color1(255,0,0);
CELL::Rgba color2(0,255,0);
CELL::Rgba color3(0,0,255);
CELL::CELLTimestamp tms;
tms.update();
raster.drawTriangle(pt[0],pt[1],pt[2],color1,color2,color3);
double mis = tms.getElapsedTimeInMicroSec();
char szBuf[128];
sprintf(szBuf,"%f ",mis);
int i = 00;
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
TextOut(hMem,10,10,szBuf,strlen(szBuf));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5938
example/lesson019-优化3/CELLMath.hpp
File diff suppressed because it is too large
View File

53
example/lesson019-优化3/CELLTimestamp.hpp

@ -0,0 +1,53 @@
#pragma once
#include <windows.h>
namespace CELL
{
class CELLTimestamp
{
public:
CELLTimestamp()
{
QueryPerformanceFrequency(&_frequency);
QueryPerformanceCounter(&_startCount);
}
~CELLTimestamp()
{}
void update()
{
QueryPerformanceCounter(&_startCount);
}
/**
* »ñÈ¡µ±Ç°Ãë
*/
double getElapsedSecond()
{
return getElapsedTimeInMicroSec() * 0.000001;
}
/**
* »ñÈ¡ºÁÃë
*/
double getElapsedTimeInMilliSec()
{
return this->getElapsedTimeInMicroSec() * 0.001;
}
/**
* »ñȡ΢Ãî
*/
double getElapsedTimeInMicroSec()
{
LARGE_INTEGER endCount;
QueryPerformanceCounter(&endCount);
double startTimeInMicroSec = _startCount.QuadPart * (1000000.0 / _frequency.QuadPart);
double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / _frequency.QuadPart);
return endTimeInMicroSec - startTimeInMicroSec;
}
protected:
LARGE_INTEGER _frequency;
LARGE_INTEGER _startCount;
};
}

202
example/lesson019-优化3/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (uint*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

229
example/lesson019-优化3/Raster.h

@ -0,0 +1,229 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
Rgba _colorStart;
Rgba _colorEnd;
int _y;
public:
Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_colorStart = colorStart;
_colorEnd = colorEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_colorStart = colorEnd;
_colorEnd = colorStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
Rgba _color1;
int _x2;
int _y2;
Rgba _color2;
Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_color1 = color1;
_x2 = x2;
_y2 = y2;
_color2 = color2;
}
else
{
_x1 = x2;
_y1 = y2;
_color1 = color2;
_x2 = x1;
_y2 = y1;
_color2 = color1;
}
}
};
class Raster
{
public:
uint* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
bool isInRect(int2 pt)
{
if (pt.x >= 0 && pt.x <= _width && pt.y >= 0 && pt.y <= _height)
{
return true;
}
return false;
}
void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
{
// if ((!isInRect(p0)) && (!isInRect(p1)) && (!isInRect(p2)))
// {
// return;
// }
Ege eges[3] =
{
Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
};
int iMax = 0;
int length = eges[0]._y2 - eges[0]._y1;
for (int i = 1 ;i < 3 ; ++ i)
{
int len = eges[i]._y2 - eges[i]._y1;
if (len > length)
{
length = len;
iMax = i;
}
}
int iShort1 = (iMax + 1)%3;
int iShort2 = (iMax + 2)%3;
drawEge(eges[iMax],eges[iShort1]);
drawEge(eges[iMax],eges[iShort2]);
}
void drawEge(const Ege& e1,const Ege& e2)
{
float yOffset1 = e1._y2 - e1._y1;
if (yOffset1 == 0)
{
return;
}
float yOffset = e2._y2 - e2._y1;
if (yOffset == 0)
{
return;
}
float xOffset = e2._x2 - e2._x1;
float scale = 0;
float step = 1.0f/yOffset;
int startY = tmax<int>(e2._y1,0);
int endY = tmin<int>(e2._y2,_height);
scale += (startY - e2._y1)/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
float step1 = 1.0f/yOffset1;
int startY1 = tmax<int>(e1._y1,0);
int endY1 = tmin<int>(e1._y2,_height);
scale1 += (startY1 - e1._y1)/yOffset1;
for (int y = startY ; y < endY ; ++ y)
{
int x1 = e1._x1 + (int)(scale1 * xOffset1);
int x2 = e2._x1 + (int)(scale * xOffset);
Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
Span span(x1,x2,y,color1,color2);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
float length = span._xEnd - span._xStart;
float scale = 0;
float step = 1.0f/length;
for (int x = span._xStart ; x < span._xEnd; ++ x)
{
Rgba color = colorLerp(
span._colorStart
,span._colorEnd
,scale
);
scale += step;
setPixel(x,span._y,color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color._color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color._color;
}
};
}

211
example/lesson019-优化3/lesson019-优化3.vcproj

@ -0,0 +1,211 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson019-优化3"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C019}"
RootNamespace="lesson019-优化3"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
EnableEnhancedInstructionSet="0"
FloatingPointModel="0"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="源文件"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson019.cpp"
>
</File>
</Filter>
<Filter
Name="头文件"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="资源文件"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

142
example/lesson019-优化3/lesson019.cpp

@ -0,0 +1,142 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
#include "CELLTimestamp.hpp"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::int2 pt[3] =
{
CELL::int2(230,-80),
CELL::int2(10,280),
CELL::int2(600,280),
};
CELL::Rgba color1(255,0,0);
CELL::Rgba color2(0,255,0);
CELL::Rgba color3(0,0,255);
CELL::CELLTimestamp tms;
tms.update();
raster.drawTriangle(pt[0],pt[1],pt[2],color1,color2,color3);
double mis = tms.getElapsedTimeInMicroSec();
char szBuf[128];
sprintf(szBuf,"%f ",mis);
int i = 00;
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
TextOut(hMem,10,10,szBuf,strlen(szBuf));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

5938
example/lesson020-优化4/CELLMath.hpp
File diff suppressed because it is too large
View File

53
example/lesson020-优化4/CELLTimestamp.hpp

@ -0,0 +1,53 @@
#pragma once
#include <windows.h>
namespace CELL
{
class CELLTimestamp
{
public:
CELLTimestamp()
{
QueryPerformanceFrequency(&_frequency);
QueryPerformanceCounter(&_startCount);
}
~CELLTimestamp()
{}
void update()
{
QueryPerformanceCounter(&_startCount);
}
/**
* »ñÈ¡µ±Ç°Ãë
*/
double getElapsedSecond()
{
return getElapsedTimeInMicroSec() * 0.000001;
}
/**
* »ñÈ¡ºÁÃë
*/
double getElapsedTimeInMilliSec()
{
return this->getElapsedTimeInMicroSec() * 0.001;
}
/**
* »ñȡ΢Ãî
*/
double getElapsedTimeInMicroSec()
{
LARGE_INTEGER endCount;
QueryPerformanceCounter(&endCount);
double startTimeInMicroSec = _startCount.QuadPart * (1000000.0 / _frequency.QuadPart);
double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / _frequency.QuadPart);
return endTimeInMicroSec - startTimeInMicroSec;
}
protected:
LARGE_INTEGER _frequency;
LARGE_INTEGER _startCount;
};
}

202
example/lesson020-优化4/Raster.cpp

@ -0,0 +1,202 @@
#include "Raster.h"
namespace CELL
{
Raster::Raster( int w,int h,void* buffer )
{
_width = w;
_height = h;
_buffer = (uint*)buffer;
}
Raster::~Raster( void )
{
}
void Raster::clear()
{
memset(_buffer,0,_width * _height * sizeof(Rgba));
}
void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
{
switch(ptSize)
{
case 1:
setPixel(x,y,color);
break;
case 2:
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
case 3:
setPixel(x - 1, y - 1,color);
setPixel(x + 0, y - 1,color);
setPixel(x + 1, y - 1,color);
setPixel(x - 1, y + 0,color);
setPixel(x + 0, y + 0,color);
setPixel(x + 1, y + 0,color);
setPixel(x - 1, y + 1,color);
setPixel(x + 0, y + 1,color);
setPixel(x + 1, y + 1,color);
break;
}
}
void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
{
float xOffset = pt1.x - pt2.x;
float yOffset = pt1.y - pt2.y;
if (xOffset == 0 && yOffset == 0)
{
setPixel(pt1.x,pt1.y,color1);
}
if (fabs(xOffset) > fabs(yOffset))
{
float xMin;
float xMax;
if (pt1.x < pt2.x)
{
xMin = pt1.x;
xMax = pt2.x;
}
else
{
xMin = pt2.x;
xMax = pt1.x;
}
float lenth = xMax - xMin;
float slope = yOffset / xOffset;
for (float x = xMin; x <= xMax ; x += 1.0f)
{
float y = pt1.y + (x - pt1.x) * slope;
float scaler = (x - xMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
else
{
float yMin;
float yMax;
if (pt1.y < pt2.y)
{
yMin = pt1.y;
yMax = pt2.y;
}
else
{
yMin = pt2.y;
yMax = pt1.y;
}
float lenth = yMax - yMin;
float slope = xOffset / yOffset;
for (float y = yMin; y <= yMax ; y += 1.0f)
{
float x = pt1.x + (y - pt1.y) * slope;
float scaler = (y - yMin)/lenth;
Rgba color = colorLerp(color1,color2,scaler);
setPixel(x,y,color);
}
}
}
void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
{
switch (mode)
{
case DM_POINTS:
{
for (int i = 0 ;i < count ; ++ i)
{
drawPoints(points[i],_color);
}
}
break;
case DM_LINES:
{
count = count/2 * 2;
for (int i = 0 ;i < count ; i += 2)
{
drawLine(points[i],points[i + 1],_color,_color);
}
}
break;
case DM_LINE_LOOP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
drawLine(points[0],points[count - 1],_color,_color);
}
break;
case DM_LINE_STRIP:
{
drawLine(points[0],points[1],_color,_color);
for (int i = 2 ;i < count ; ++ i)
{
drawLine(points[i - 1],points[i],_color,_color);
}
}
break;
default:
break;
}
}
void Raster::drawFilleRect( int startX,int startY,int w,int h )
{
int left = tmax<int>(startX,0);
int top = tmax<int>(startY,0);
int right = tmin<int>(startX + w,_width);
int bottom = tmin<int>(startY + h,_height);
for (int x = left ; x < right ; ++ x)
{
for (int y = top ; y < bottom ; ++ y)
{
setPixelEx(x,y,_color);
}
}
}
void Raster::drawRect( const int2* points,const Rgba* colors )
{
int left = tmax<int>(points[0].x,0);
int top = tmax<int>(points[0].y,0);
int right = tmin<int>(points[2].x,_width);
int bottom = tmin<int>(points[2].y,_height);
float w = right - left;
float h = bottom - top;
for (int x = left ; x < right ; ++ x)
{
Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
for (int y = top ; y < bottom ; ++ y)
{
Rgba color = colorLerp(color1,color2,(y - top)/h);
setPixelEx(x,y,color);
}
}
}
}

232
example/lesson020-优化4/Raster.h

@ -0,0 +1,232 @@
#pragma once
#include "CELLMath.hpp"
namespace CELL
{
enum DRAWMODE
{
DM_POINTS = 0,
DM_LINES = 1,
DM_LINE_LOOP = 2,
DM_LINE_STRIP = 3,
};
class Span
{
public:
int _xStart;
int _xEnd;
Rgba _colorStart;
Rgba _colorEnd;
int _y;
public:
Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
{
if (xStart < xEnd)
{
_xStart = xStart;
_xEnd = xEnd;
_colorStart = colorStart;
_colorEnd = colorEnd;
_y = y;
}
else
{
_xStart = _xEnd;
_xEnd = _xStart;
_colorStart = colorEnd;
_colorEnd = colorStart;
_y = y;
}
}
};
class Ege
{
public:
int _x1;
int _y1;
Rgba _color1;
int _x2;
int _y2;
Rgba _color2;
Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
{
if (y1 < y2)
{
_x1 = x1;
_y1 = y1;
_color1 = color1;
_x2 = x2;
_y2 = y2;
_color2 = color2;
}
else
{
_x1 = x2;
_y1 = y2;
_color1 = color2;
_x2 = x1;
_y2 = y1;
_color2 = color1;
}
}
};
class Raster
{
public:
uint* _buffer;
int _width;
int _height;
Rgba _color;
public:
Raster(int w,int h,void* buffer);
~Raster(void);
void clear();
void drawPoint(int x,int y, Rgba color,int ptSize);
void drawArrays(DRAWMODE mode,const float2* points,int count);
void drawFilleRect(int startX,int startY,int w,int h);
void drawRect(const int2* points,const Rgba* colors);
public:
bool isInRect(int2 pt)
{
if (pt.x >= 0 && pt.x <= _width && pt.y >= 0 && pt.y <= _height)
{
return true;
}
return false;
}
void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
{
// if ((!isInRect(p0)) && (!isInRect(p1)) && (!isInRect(p2)))
// {
// return;
// }
Ege eges[3] =
{
Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
};
int iMax = 0;
int length = eges[0]._y2 - eges[0]._y1;
for (int i = 1 ;i < 3 ; ++ i)
{
int len = eges[i]._y2 - eges[i]._y1;
if (len > length)
{
length = len;
iMax = i;
}
}
int iShort1 = (iMax + 1)%3;
int iShort2 = (iMax + 2)%3;
drawEge(eges[iMax],eges[iShort1]);
drawEge(eges[iMax],eges[iShort2]);
}
void drawEge(const Ege& e1,const Ege& e2)
{
float yOffset1 = e1._y2 - e1._y1;
if (yOffset1 == 0)
{
return;
}
float yOffset = e2._y2 - e2._y1;
if (yOffset == 0)
{
return;
}
float xOffset = e2._x2 - e2._x1;
float scale = 0;
float step = 1.0f/yOffset;
int startY = tmax<int>(e2._y1,0);
int endY = tmin<int>(e2._y2,_height);
scale += (startY - e2._y1)/yOffset;
float xOffset1 = e1._x2 - e1._x1;
float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
float step1 = 1.0f/yOffset1;
int startY1 = tmax<int>(e1._y1,0);
int endY1 = tmin<int>(e1._y2,_height);
scale1 += (startY1 - e1._y1)/yOffset1;
for (int y = startY ; y < endY ; ++ y)
{
int x1 = e1._x1 + (int)(scale1 * xOffset1);
int x2 = e2._x1 + (int)(scale * xOffset);
Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
Span span(x1,x2,y,color1,color2);
drawSpan(span);
scale += step;
scale1 += step1;
}
}
void drawSpan(const Span& span)
{
float length = span._xEnd - span._xStart;
float scale = 0;
float step = 1.0f/length;
int startX = tmax<int>(span._xStart,0);
int endX = tmin<int>(span._xEnd,_width);
scale += (startX - span._xStart)/length;
for (int x = startX ; x < endX; ++ x)
{
Rgba color = colorLerp(
span._colorStart
,span._colorEnd
,scale
);
scale += step;
setPixelEx(x,span._y,color);
}
}
void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
void drawPoints(float2 pt1,Rgba4Byte color)
{
}
inline void setPixelEx(unsigned x,unsigned y,Rgba color)
{
_buffer[y * _width + x] = color._color;
}
inline void setPixel(unsigned x,unsigned y,Rgba color)
{
if (x >= _width || y >= _height)
{
return;
}
_buffer[y * _width + x] = color._color;
}
};
}

142
example/lesson020-优化4/lesson019.cpp

@ -0,0 +1,142 @@
#include <windows.h>
#include <tchar.h>
#include "Raster.h"
#include "CELLTimestamp.hpp"
LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
break;
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// 1 ×¢²á´°¿ÚÀà
::WNDCLASSEXA winClass;
winClass.lpszClassName = "Raster";
winClass.cbSize = sizeof(::WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
winClass.lpfnWndProc = windowProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassExA(&winClass);
// 2 ´´½¨´°¿Ú
HWND hWnd = CreateWindowExA(
NULL,
"Raster",
"Raster",
WS_OVERLAPPEDWINDOW,
0,
0,
480,
320,
0,
0,
hInstance,
0
);
UpdateWindow( hWnd );
ShowWindow(hWnd,SW_SHOW);
RECT rt = {0};
GetClientRect(hWnd,&rt);
int width = rt.right - rt.left;
int height = rt.bottom - rt.top;
void* buffer = 0;
HDC hDC = GetDC(hWnd);
HDC hMem = ::CreateCompatibleDC(hDC);
BITMAPINFO bmpInfor;
bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfor.bmiHeader.biWidth = width;
bmpInfor.bmiHeader.biHeight = -height;
bmpInfor.bmiHeader.biPlanes = 1;
bmpInfor.bmiHeader.biBitCount = 32;
bmpInfor.bmiHeader.biCompression = BI_RGB;
bmpInfor.bmiHeader.biSizeImage = 0;
bmpInfor.bmiHeader.biXPelsPerMeter = 0;
bmpInfor.bmiHeader.biYPelsPerMeter = 0;
bmpInfor.bmiHeader.biClrUsed = 0;
bmpInfor.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
SelectObject(hMem,hBmp);
CELL::Raster raster(width,height,buffer);
MSG msg = {0};
while(true)
{
if (msg.message == WM_DESTROY
||msg.message == WM_CLOSE
||msg.message == WM_QUIT)
{
break;
}
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
raster.clear();
CELL::int2 pt[3] =
{
CELL::int2(230,-80),
CELL::int2(10,280),
CELL::int2(600,280),
};
CELL::Rgba color1(255,0,0);
CELL::Rgba color2(0,255,0);
CELL::Rgba color3(0,0,255);
CELL::CELLTimestamp tms;
tms.update();
raster.drawTriangle(pt[0],pt[1],pt[2],color1,color2,color3);
double mis = tms.getElapsedTimeInMicroSec();
char szBuf[128];
sprintf(szBuf,"%f ",mis);
int i = 00;
memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
TextOut(hMem,10,10,szBuf,strlen(szBuf));
BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
}
return 0;
}

211
example/lesson020-优化4/lesson020-优化4.vcproj

@ -0,0 +1,211 @@
<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="lesson020-优化4"
ProjectGUID="{4880A4A6-8888-4A22-910F-F65223B5C020}"
RootNamespace="lesson020-优化4"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
IntermediateDirectory="../../temp/$(ConfigurationName)/$(ProjectName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="0"
EnableFunctionLevelLinking="true"
EnableEnhancedInstructionSet="0"
FloatingPointModel="0"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="FreeImage.lib"
OutputFile="../../bin/$(ConfigurationName)/$(ProjectName).exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../dependencies"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="源文件"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\lesson019.cpp"
>
</File>
</Filter>
<Filter
Name="头文件"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\CELLMath.hpp"
>
</File>
<File
RelativePath=".\Raster.cpp"
>
</File>
<File
RelativePath=".\Raster.h"
>
</File>
</Filter>
<Filter
Name="资源文件"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

5938
example/lesson200-drawImage/CELLMath.hpp
File diff suppressed because it is too large
View File

53
example/lesson200-drawImage/CELLTimestamp.hpp

@ -0,0 +1,53 @@
#pragma once
#include <windows.h>
namespace CELL
{
class CELLTimestamp
{
public:
CELLTimestamp()
{
QueryPerformanceFrequency(&_frequency);
QueryPerformanceCounter(&_startCount);
}
~CELLTimestamp()
{}
void update()
{
QueryPerformanceCounter(&_startCount);
}
/**
* »ñÈ¡µ±Ç°Ãë
*/
double getElapsedSecond()
{
return getElapsedTimeInMicroSec() * 0.000001;
}
/**
* »ñÈ¡ºÁÃë
*/
double getElapsedTimeInMilliSec()
{
return this->getElapsedTimeInMicroSec() * 0.001;
}
/**
* »ñȡ΢Ãî
*/
double getElapsedTimeInMicroSec()
{
LARGE_INTEGER endCount;
QueryPerformanceCounter(&endCount);
double startTimeInMicroSec = _startCount.QuadPart * (1000000.0 / _frequency.QuadPart);
double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / _frequency.QuadPart);
return endTimeInMicroSec - startTimeInMicroSec;
}
protected:
LARGE_INTEGER _frequency;
LARGE_INTEGER _startCount;
};
}

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save