Beispiel #1
0
int InteractiveDemo( void )
{
	if (SDL_Init(SDL_INIT_VIDEO) == -1) {
		std::cout << SDL_GetError() << std::endl;
		return 1;
	}
	atexit(SDL_Quit);
	if (SDL_SetVideoMode(512, 512, 24, SDL_SWSURFACE|SDL_DOUBLEBUF) == NULL) {
		std::cout << SDL_GetError() << std::endl;
		return 1;
	}
	SDL_WM_SetCaption("SWSL test", NULL);

	const float side_len = 300.0f;
	const float base_len = side_len * sin(mmlPI / 3.0f);

	mmlVector<2> a_pos  = mmlVector<2>(side_len * 0.5f,     0.0f);
	mmlVector<2> b_pos  = mmlVector<2>(side_len,        base_len);
	mmlVector<2> c_pos  = mmlVector<2>(    0.0f,        base_len);
	mmlVector<2> center = (a_pos + b_pos + c_pos) / 3.0f;
	a_pos -= center;
	b_pos -= center;
	c_pos -= center;

	SDL_Event        event;
	swsl::Shader     shader;
	bool             quit = false;
	bool             reload_shader = true;
	bool             shader_status = false;
	swsl::Rasterizer raster;
	Printer          p;

	p.SetColor(0, 255, 0);

	raster.CreateBuffers(video->w, video->h);
	raster.SetShader(&shader);

	while (!quit) {

		Uint32 frame_start = SDL_GetTicks();

		if (reload_shader) {
			reload_shader = false;
			shader_status = LoadShader("../swsl_samples/interactive.swsl", shader);
		}

		while (SDL_PollEvent(&event)) {
			switch (event.type) {
			case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_r) {
					reload_shader = true;
				}
				break;
			case SDL_QUIT: quit = true; break;
			}
		}

		Vertex<3> a, b, c;

		//mmlMatrix<2,2> rmat = mml2DRotationMatrix((float)SDL_GetTicks() / 1000.0f);
		mmlMatrix<2,2> rmat = mmlMatrix<2,2>::IdentityMatrix();
		mmlVector<2>   at   = a_pos * rmat;
		mmlVector<2>   bt   = b_pos * rmat;
		mmlVector<2>   ct   = c_pos * rmat;

		a.coord.x = round(at[0]) + video->w / 2;
		a.coord.y = round(at[1]) + video->h / 2;
		b.coord.x = round(bt[0]) + video->w / 2;
		b.coord.y = round(bt[1]) + video->h / 2;
		c.coord.x = round(ct[0]) + video->w / 2;
		c.coord.y = round(ct[1]) + video->h / 2;

		a.attributes[0] = 1.0f;
		a.attributes[1] = 0.0f;
		a.attributes[2] = 0.0f;
		b.attributes[0] = 0.0f;
		b.attributes[1] = 1.0f;
		b.attributes[2] = 0.0f;
		c.attributes[0] = 0.0f;
		c.attributes[1] = 0.0f;
		c.attributes[2] = 1.0f;

		//float rgb[] = { 1.0f, 1.0f, 1.0f };
		//raster.ClearBuffers(rgb);
		raster.ClearBuffers();

		Uint32 render_start = SDL_GetTicks();
		raster.FillTriangle(a.coord, b.coord, c.coord, a.attributes, b.attributes, c.attributes);
		Uint32 render_end = SDL_GetTicks();
		Uint32 render_time = render_end - render_start;

		raster.WriteColorBuffer((mtlByte*)video->pixels, video->format->BytesPerPixel, ByteOrder());

		if (!shader_status) {
			p.SetColor(255, 0, 0);
			p.Print("Shader execution failed");
			p.Newline();
		} else {
			p.SetColor(0, 255, 0);
		}
		p.Print("Press \'r\' to reload shader");
		p.Newline();
		p.Print("Render time: ");

		p.Print((int)render_time);
		p.Print("/");
		Uint32 frame_end = SDL_GetTicks();
		Uint32 frame_time = frame_end - frame_start;
		p.Print((int)frame_time);
		p.Print(" ms");
		p.ResetCaret();

		SDL_Flip(video);
		if ((int)(25 - render_time) > 0) {
			SDL_Delay(25 - render_time);
		}

	}

	SDL_Quit();

	return 0;
}
int main ()
{
	Printer* printer = Printer::GetInstance();
	printer->Print( "report.doc" );
}