14 #include <SDL/SDL_ttf.h>
45 template<
int t_ColorFormat,
int t_Platform,
int t_RendererPlatform >
47 SDL_Surface* pSurface,
49 Synchronized::Heap< t_Platform >& heap,
50 Synchronized::TextureHeap< t_ColorFormat, t_Platform >& textureHeap,
52 Renderer::State< t_RendererPlatform >
const& renderer,
56 if ( SDL_FillRect( pSurface, NULL, SDL_MapRGB( pSurface->format, 0, 0, 0 ) ) != 0 )
59 if ( SDL_LockSurface( pSurface ) != 0 )
66 typename Synchronized::Heap< t_Platform >::template Pointer< Octree::Node< t_ColorFormat, t_Platform > >
const pSubtractedRoot = Octree::Subtract(
75 textureHeap.Synchronize();
77 typename Synchronized::Heap< t_Platform >::template Pointer< Octree::Node< t_ColorFormat, t_Platform > >
const& pSubtractedRoot = pRoot;
81 static_cast< uint8_t* >( pSurface->pixels ),
92 SDL_UnlockSurface( pSurface );
95 SDL_UnlockSurface( pSurface );
97 {
char strings[ 4 ][ 128 ];
100 unsigned int const threads = omp_get_max_threads();
102 unsigned int const threads = 1;
104 snprintf( strings[ 0 ],
ARRAYLENGTH( strings[ 0 ] ),
"threads = %u, resolution = %dx%d", threads, pSurface->w, pSurface->h );
105 snprintf( strings[ 1 ],
ARRAYLENGTH( strings[ 1 ] ),
"%.2f Mrays / frame", pSurface->w * pSurface->h / 1000000.0 );
106 snprintf( strings[ 2 ],
ARRAYLENGTH( strings[ 2 ] ),
"%.2f frames / sec", g_fps );
107 snprintf( strings[ 3 ],
ARRAYLENGTH( strings[ 3 ] ),
"%.2f Mrays / sec", g_rps / 1000000 );
109 SDL_Color
const textColor = { 0xff, 0xff, 0xff };
113 for (
unsigned int ii = 0; ii <
ARRAYLENGTH( strings ); ++ii ) {
115 strings[ ii ][
ARRAYLENGTH( strings[ ii ] ) - 1 ] =
'\0';
117 std::unique_ptr< SDL_Surface, void (*)( SDL_Surface* ) > pTextSurface( TTF_RenderText_Blended( pFont, strings[ ii ], textColor ), SDL_FreeSurface );
118 if ( pTextSurface.get() == NULL )
121 SDL_Rect destinationRect = { xx, yy,
static_cast< Uint16
>( pTextSurface->w ), static_cast< Uint16 >( pTextSurface->h ) };
122 if ( SDL_BlitSurface( pTextSurface.get(), NULL, pSurface, &destinationRect ) != 0 )
124 yy += pTextSurface->h;
137 int main(
int argc,
char* argv[] ) {
139 int result = EXIT_SUCCESS;
147 #if ( SETTING_OPENCL != 0 )
148 std::shared_ptr< OpenCL::State > pState(
new OpenCL::State() );
150 Synchronized::TextureHeap< COLOR_FORMAT, PLATFORM_OPENCL > textureHeap( pState, 27 );
153 #else // SETTING_OPENCL
155 Synchronized::TextureHeap< COLOR_FORMAT, PLATFORM_HOST > textureHeap;
158 #endif // SETTING_OPENCL
160 textureHeap.Synchronize();
170 MOVEMENT_KEY_MOVE_FORWARD = 0,
171 MOVEMENT_KEY_MOVE_BACK,
172 MOVEMENT_KEY_MOVE_DOWN,
173 MOVEMENT_KEY_MOVE_UP,
174 MOVEMENT_KEY_MOVE_RIGHT,
175 MOVEMENT_KEY_MOVE_LEFT,
176 MOVEMENT_KEY_TURN_COUNTERCLOCKWISE,
177 MOVEMENT_KEY_TURN_CLOCKWISE,
178 MOVEMENT_KEY_TURN_RIGHT,
179 MOVEMENT_KEY_TURN_LEFT,
180 MOVEMENT_KEY_TURN_UP,
181 MOVEMENT_KEY_TURN_DOWN,
182 MOVEMENT_KEY_ZOOM_IN,
183 MOVEMENT_KEY_ZOOM_OUT,
187 bool movementKeys[ MOVEMENT_KEYS_SIZE ];
188 std::fill( movementKeys, movementKeys +
ARRAYLENGTH( movementKeys ),
false );
190 double const fpsRate = 0.95;
191 double const rateTurn = 0.8;
192 double const rateZoom = 0.4;
193 double const rateMove = 0.2;
197 if ( SDL_Init( SDL_INIT_VIDEO ) != 0 )
201 std::unique_ptr< SDL_Surface, void (*)( SDL_Surface* ) > pSurface( SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF ), SDL_FreeSurface );
202 if ( pSurface.get() == NULL )
205 SDL_WM_SetCaption(
"Voxel",
"Voxel" );
209 if ( TTF_Init() != 0 )
213 std::unique_ptr< TTF_Font, void (*)( TTF_Font* ) > pFont( TTF_OpenFont(
"verdanab.ttf", 14 ), TTF_CloseFont );
214 if ( pFont.get() == NULL )
223 {
struct timeval time;
224 gettimeofday( &time, NULL );
225 ticks = time.tv_sec +
static_cast< double >( time.tv_usec ) / 1000000;
229 for (
bool done =
false; ! done; ) {
233 Redraw( pSurface.get(), pFont.get(), heap, textureHeap, pRoot, renderer, camera );
234 SDL_Flip( pSurface.get() );
238 double const ticksOld = ticks;
239 {
struct timeval time;
240 gettimeofday( &time, NULL );
241 ticks = time.tv_sec +
static_cast< double >( time.tv_usec ) / 1000000;
243 double const deltaSeconds = ticks - ticksOld;
245 frames = fpsRate * frames + ( 1 - fpsRate );
246 rays = fpsRate * rays + ( 1 - fpsRate ) * pSurface->w * pSurface->h;
247 seconds = fpsRate * seconds + ( 1 - fpsRate ) * deltaSeconds;
249 g_fps = frames / seconds;
250 g_rps = rays / seconds;
254 while ( SDL_PollEvent( &event ) ) {
256 switch( event.type ) {
260 switch( event.key.keysym.sym ) {
261 case SDLK_w: { movementKeys[ MOVEMENT_KEY_MOVE_FORWARD ] =
false;
break; }
262 case SDLK_s: { movementKeys[ MOVEMENT_KEY_MOVE_BACK ] =
false;
break; }
263 case SDLK_PAGEDOWN: { movementKeys[ MOVEMENT_KEY_MOVE_DOWN ] =
false;
break; }
264 case SDLK_PAGEUP: { movementKeys[ MOVEMENT_KEY_MOVE_UP ] =
false;
break; }
265 case SDLK_d: { movementKeys[ MOVEMENT_KEY_MOVE_RIGHT ] =
false;
break; }
266 case SDLK_a: { movementKeys[ MOVEMENT_KEY_MOVE_LEFT ] =
false;
break; }
267 case SDLK_q: { movementKeys[ MOVEMENT_KEY_TURN_COUNTERCLOCKWISE ] =
false;
break; }
268 case SDLK_e: { movementKeys[ MOVEMENT_KEY_TURN_CLOCKWISE ] =
false;
break; }
269 case SDLK_RIGHT: { movementKeys[ MOVEMENT_KEY_TURN_RIGHT ] =
false;
break; }
270 case SDLK_LEFT: { movementKeys[ MOVEMENT_KEY_TURN_LEFT ] =
false;
break; }
271 case SDLK_UP: { movementKeys[ MOVEMENT_KEY_TURN_UP ] =
false;
break; }
272 case SDLK_DOWN: { movementKeys[ MOVEMENT_KEY_TURN_DOWN ] =
false;
break; }
273 case SDLK_HOME: { movementKeys[ MOVEMENT_KEY_ZOOM_OUT ] =
false;
break; }
274 case SDLK_END: { movementKeys[ MOVEMENT_KEY_ZOOM_IN ] =
false;
break; }
275 case SDLK_ESCAPE: { done =
true;
break; }
283 switch( event.key.keysym.sym ) {
284 case SDLK_w: { movementKeys[ MOVEMENT_KEY_MOVE_FORWARD ] =
true;
break; }
285 case SDLK_s: { movementKeys[ MOVEMENT_KEY_MOVE_BACK ] =
true;
break; }
286 case SDLK_PAGEDOWN: { movementKeys[ MOVEMENT_KEY_MOVE_DOWN ] =
true;
break; }
287 case SDLK_PAGEUP: { movementKeys[ MOVEMENT_KEY_MOVE_UP ] =
true;
break; }
288 case SDLK_d: { movementKeys[ MOVEMENT_KEY_MOVE_RIGHT ] =
true;
break; }
289 case SDLK_a: { movementKeys[ MOVEMENT_KEY_MOVE_LEFT ] =
true;
break; }
290 case SDLK_q: { movementKeys[ MOVEMENT_KEY_TURN_COUNTERCLOCKWISE ] =
true;
break; }
291 case SDLK_e: { movementKeys[ MOVEMENT_KEY_TURN_CLOCKWISE ] =
true;
break; }
292 case SDLK_RIGHT: { movementKeys[ MOVEMENT_KEY_TURN_RIGHT ] =
true;
break; }
293 case SDLK_LEFT: { movementKeys[ MOVEMENT_KEY_TURN_LEFT ] =
true;
break; }
294 case SDLK_UP: { movementKeys[ MOVEMENT_KEY_TURN_UP ] =
true;
break; }
295 case SDLK_DOWN: { movementKeys[ MOVEMENT_KEY_TURN_DOWN ] =
true;
break; }
296 case SDLK_HOME: { movementKeys[ MOVEMENT_KEY_ZOOM_OUT ] =
true;
break; }
297 case SDLK_END: { movementKeys[ MOVEMENT_KEY_ZOOM_IN ] =
true;
break; }
313 {
double const move = ( movementKeys[ MOVEMENT_KEY_MOVE_FORWARD ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_MOVE_BACK ] ? 1.0 : 0.0 );
316 double const rate = move * rateMove * deltaSeconds;
320 {
double const move = ( movementKeys[ MOVEMENT_KEY_MOVE_DOWN ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_MOVE_UP ] ? 1.0 : 0.0 );
323 double const rate = move * rateMove * deltaSeconds;
327 {
double const move = ( movementKeys[ MOVEMENT_KEY_MOVE_RIGHT ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_MOVE_LEFT ] ? 1.0 : 0.0 );
330 double const rate = move * rateMove * deltaSeconds;
334 {
double const turn = ( movementKeys[ MOVEMENT_KEY_TURN_CLOCKWISE ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_TURN_COUNTERCLOCKWISE ] ? 1.0 : 0.0 );
337 double const rate = turn * rateTurn * deltaSeconds;
341 {
double const turn = ( movementKeys[ MOVEMENT_KEY_TURN_RIGHT ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_TURN_LEFT ] ? 1.0 : 0.0 );
344 double const rate = turn * rateTurn * deltaSeconds;
348 {
double const turn = ( movementKeys[ MOVEMENT_KEY_TURN_DOWN ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_TURN_UP ] ? 1.0 : 0.0 );
351 double const rate = turn * rateTurn * deltaSeconds;
355 {
double const zoom = ( movementKeys[ MOVEMENT_KEY_ZOOM_OUT ] ? 1.0 : 0.0 ) - ( movementKeys[ MOVEMENT_KEY_ZOOM_IN ] ? 1.0 : 0.0 );
358 double const rate = zoom * rateZoom * deltaSeconds;
364 catch( std::exception& exception ) {
366 std::cerr <<
"caught exception: " << exception.what() << std::endl;
367 result = EXIT_FAILURE;
371 std::cerr <<
"caught exception: <unknown error>" << std::endl;
372 result = EXIT_FAILURE;