initial hilbert commit
git-svn-id: svn://anubis/misc/hilbert@14 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
bac8bbdd9c
commit
270a1ac5a7
13
Makefile
Normal file
13
Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
CC := gcc
|
||||
CFLAGS := -O3 -ansi -pedantic -Wall `sdl-config --cflags`
|
||||
LDFLAGS := -lGL -lGLU `sdl-config --libs`
|
||||
TARGET := hilbert
|
||||
OBJS := hilbert.o
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
gcc -o $@ $^ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
-rm -f $(FILE) *~ *.o
|
149
hilbert.c
Normal file
149
hilbert.c
Normal file
@ -0,0 +1,149 @@
|
||||
/* Hilbert curve drawing program
|
||||
* by Josh Holtrop
|
||||
*/
|
||||
|
||||
/* includes */
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
/* constants */
|
||||
#define SEGMENTS 16
|
||||
#define WIDTH 800
|
||||
#define HEIGHT 600
|
||||
|
||||
/* functions */
|
||||
void usage(void);
|
||||
void initgl(void);
|
||||
void display(void);
|
||||
void mainloop(void);
|
||||
void drawHilbert();
|
||||
|
||||
/* global variables */
|
||||
int level = 2;
|
||||
float size = 0.4;
|
||||
SDL_Surface * sdlScreen;
|
||||
int winWidth = WIDTH;
|
||||
int winHeight = HEIGHT;
|
||||
int displayList = -1;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < argc; i++)
|
||||
{
|
||||
if (!strncmp("-l", argv[i], 2))
|
||||
{
|
||||
sscanf(argv[i] + 2, "%d", &level);
|
||||
if (level < 1)
|
||||
usage();
|
||||
}
|
||||
else if (!strncmp("-s", argv[i], 2))
|
||||
{
|
||||
sscanf(argv[i] + 2, "%f", &size);
|
||||
if (size < 0.01 || size > 0.99)
|
||||
usage();
|
||||
}
|
||||
else
|
||||
{
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
printf("Failed to initialize SDL!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
atexit(SDL_Quit);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
if (!(sdlScreen = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_OPENGL | SDL_RESIZABLE)))
|
||||
{
|
||||
printf("Failed to set video mode!\n");
|
||||
SDL_Quit();
|
||||
return 2;
|
||||
}
|
||||
SDL_WM_SetCaption(argv[0], argv[0]);
|
||||
|
||||
initgl();
|
||||
mainloop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mainloop(void)
|
||||
{
|
||||
SDL_Event event;
|
||||
display();
|
||||
while (SDL_WaitEvent(&event))
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
return;
|
||||
case SDL_KEYDOWN:
|
||||
switch (event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_VIDEORESIZE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initgl(void)
|
||||
{
|
||||
glClearColor (0.0, 0.0, 0.0, 0.0);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glShadeModel(GL_SMOOTH);
|
||||
glViewport(0, 0, winWidth, winHeight);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
gluPerspective(60.0, (GLfloat)winHeight/(GLfloat)winWidth, 1.0, 30.0);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
glTranslatef(0.0, 0.0, -3.6);
|
||||
}
|
||||
|
||||
void display(void)
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
if (displayList >= 0)
|
||||
{
|
||||
glCallList(displayList);
|
||||
}
|
||||
else
|
||||
{
|
||||
displayList = glGenLists(1);
|
||||
if (displayList < 0)
|
||||
{
|
||||
fprintf(stderr, "Error allocating display list!\n");
|
||||
exit(132);
|
||||
}
|
||||
glNewList(displayList, GL_COMPILE_AND_EXECUTE);
|
||||
drawHilbert();
|
||||
glEndList();
|
||||
}
|
||||
|
||||
SDL_GL_SwapBuffers();
|
||||
}
|
||||
|
||||
void drawHilbert()
|
||||
{
|
||||
}
|
||||
|
||||
void usage(void)
|
||||
{
|
||||
printf("Options:\n"
|
||||
" -l<level> level of Hilbert curve to draw (1-?)\n"
|
||||
" -s<size> radius of Hilbert curve (0.01-0.99)\n");
|
||||
exit(42);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user