ccdemos/common_sdl.h
branchpyrit
changeset 92 9af5c039b678
parent 90 f6a72eb99631
child 93 96d65f841791
equal deleted inserted replaced
91:9d66d323c354 92:9af5c039b678
    28 			return;
    28 			return;
    29 
    29 
    30 	Uint32 *bufp = (Uint32 *)screen->pixels;
    30 	Uint32 *bufp = (Uint32 *)screen->pixels;
    31 	for (Float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3)
    31 	for (Float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3)
    32 	{
    32 	{
    33 #ifdef NO_SSE
       
    34 		unsigned char c[3];
    33 		unsigned char c[3];
    35 		for (int i = 0; i < 3; i++)
    34 		for (int i = 0; i < 3; i++)
    36 		{
    35 		{
    37 			if (fd[i] > 1.0)
    36 			if (fd[i] > 1.0)
    38 				c[i] = 255;
    37 				c[i] = 255;
    39 			else
    38 			else
    40 				c[i] = (unsigned char)(fd[i] * 255.0);
    39 				c[i] = (unsigned char)(fd[i] * 255.0);
    41 		}
    40 		}
    42 		*bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]);
    41 		*bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]);
    43 #else
       
    44 		__m64 m = _mm_cvtps_pi16(_mm_mul_ps(_mm_set_ps1(255.0),
       
    45 			_mm_min_ps(mOne, _mm_set_ps(0, fd[2],fd[1],fd[0]))));
       
    46 		*bufp = SDL_MapRGB(screen->format, ((char*)&m)[0], ((char*)&m)[2], ((char*)&m)[4]);
       
    47 #endif
       
    48 		bufp++;
    42 		bufp++;
    49 	}
    43 	}
    50 
    44 
    51 	if (SDL_MUSTLOCK(screen))
    45 	if (SDL_MUSTLOCK(screen))
    52 		SDL_UnlockSurface(screen);
    46 		SDL_UnlockSurface(screen);
   106 					if (event.key.keysym.sym == SDLK_RIGHT) {
   100 					if (event.key.keysym.sym == SDLK_RIGHT) {
   107 						roty = -0.01;
   101 						roty = -0.01;
   108 						break;
   102 						break;
   109 					}
   103 					}
   110 					if (event.key.keysym.sym == SDLK_DOWN) {
   104 					if (event.key.keysym.sym == SDLK_DOWN) {
   111 						rotx = +0.01;
   105 						rotx = +0.01f;
   112 						break;
   106 						break;
   113 					}
   107 					}
   114 					if (event.key.keysym.sym == SDLK_UP) {
   108 					if (event.key.keysym.sym == SDLK_UP) {
   115 						rotx = -0.01;
   109 						rotx = -0.01f;
   116 						break;
   110 						break;
   117 					}
   111 					}
   118 					if (event.key.keysym.sym == SDLK_w) {
   112 					if (event.key.keysym.sym == SDLK_w) {
   119 						move = +0.5;
   113 						move = +0.5f;
   120 						break;
   114 						break;
   121 					}
   115 					}
   122 					if (event.key.keysym.sym == SDLK_s) {
   116 					if (event.key.keysym.sym == SDLK_s) {
   123 						move = -0.5;
   117 						move = -0.5f;
   124 						break;
   118 						break;
   125 					}
   119 					}
   126 					if (event.key.keysym.sym == SDLK_c) {
   120 					if (event.key.keysym.sym == SDLK_c) {
   127 						// print camera coordinates
   121 						// print camera coordinates
   128 						cout << "Camera: eye=" << cam.eye
   122 						cout << "Camera: eye=" << cam.getEye()
   129 						<< ", p=" << cam.p
   123 						<< ", p=" << cam.getp()
   130 						<< ", u=" << cam.u
   124 						<< ", u=" << cam.getu()
   131 						<< ", v=" << cam.v
   125 						<< ", v=" << cam.getv()
   132 						<< endl;
   126 						<< endl;
   133 						break;
   127 						break;
   134 					}
   128 					}
   135 					if (key_callback != NULL)
   129 					if (key_callback != NULL)
   136 						key_callback(event.key.keysym.sym, 1);
   130 						key_callback(event.key.keysym.sym, 1);
   154 				case SDL_QUIT:
   148 				case SDL_QUIT:
   155 					quit = true;
   149 					quit = true;
   156 			}
   150 			}
   157 		}
   151 		}
   158 		cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize());
   152 		cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize());
   159 		cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize());
   153 		cam.rotate(Quaternion(cos(rotx),
   160 		cam.u.y = 0;
   154 			cam.getu()[0]*sin(rotx),0,cam.getu()[2]*sin(rotx)).normalize());
   161 		cam.u.normalize();
   155 		//cam.u.y = 0;
       
   156 		//cam.u.normalize();
   162 		if (move != 0.0)
   157 		if (move != 0.0)
   163 			cam.move(move,0,0);
   158 			cam.move(move,0,0);
   164 		if (update_callback != NULL)
   159 		if (update_callback != NULL)
   165 			update_callback(render_buffer);
   160 			update_callback(render_buffer);
   166 		update(rt, screen, render_buffer);
   161 		update(rt, screen, render_buffer);
   167 	}
   162 	}
       
   163 
   168 	free(render_buffer);
   164 	free(render_buffer);
   169 
   165 
   170 	Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples;
   166 	Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples;
   171 	printf("averlage fps: %3d.%2d\n\n", fp100s_aver/100, fp100s_aver%100);
   167 	printf("averlage fps: %3d.%2d\n\n", fp100s_aver/100, fp100s_aver%100);
   172 	SDL_Quit();
   168 	SDL_Quit();