めも

private import SDL;
private import opengl;
private import openglu;
private import std.math;

void main()
{
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		throw new Error("SDL Init Failed.");
	}
	SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
	
	// OpenGL初期化
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glViewport(0, 0, 640, 480);
	gluPerspective(30, 640.0/480, 1, 1000);

	glClearColor(0, 0, 0, 0);
	// イベントループ
	float x, y;
	x = 0, y = 0;
	
	float r = 0;
	bool done = false;
	SDL_Event e;
	while ( !done ) {
		while ( SDL_PollEvent(&e) ) {
			done = e.type == SDL_QUIT;
		}
		glClear(GL_COLOR_BUFFER_BIT);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(0, 480, 50, 0, 0, 0, 0, 1, 0);
		drawWall();

		if(isPress(SDLK_LEFT))  x -= 5;
		if(isPress(SDLK_UP))    y -= 5;
		if(isPress(SDLK_RIGHT)) x += 5;
		if(isPress(SDLK_DOWN))  y += 5;
		glTranslatef(x, 0, y);
		r += 1;
		glRotated(r, 0, 5, 0);
		
		
		glBegin(GL_POLYGON);
		{
			float size = 12;
			glColor3f(1, 0, 0);
			glVertex3d(-size/2, 0, -size/2);
			glColor3f(0, 1, 0);
			glVertex3d(size/2,  0, -size/2);
			glColor3f(0, 0, 1);
			glVertex3d(size/2,  0, size/2);
			glColor3f(1, 1, 0);
			glVertex3d(-size/2, 0, size/2);
		}
		glEnd();
		
		glFlush();
		SDL_GL_SwapBuffers();
		SDL_Delay(1000/30);
	}
	SDL_Quit();

}

void drawWall()
{
	float w = 160;
	float h = 240;
	foreach(y; [-10.0, 10.0])
	{
		drawLine(-w/2, y, -h/2,  w/2, y, -h/2);
		drawLine( w/2, y, -h/2,  w/2, y,  h/2);
		drawLine( w/2, y,  h/2, -w/2, y,  h/2);
		drawLine(-w/2, y,  h/2, -w/2, y, -h/2);
	}
}

void drawLine(float x1, float y1, float z1, float x2, float y2, float z2)
{
	glBegin(GL_LINES);
	{
		glVertex3d(x1, y1, z1);
		glVertex3d(x2, y2, z2);
	}
	glEnd();
}

bool isPress(int id)
{
	Uint8* keys = SDL_GetKeyState(null);
	return keys[id] == SDL_PRESSED;
}