Added sub-project dethphysics
[hammerdown.git] / dethphysics / vector.c~
blobcdf741a2720ec019b9787b585be8ec7f469d5e8f
1 //headers
2 #include <SDL/SDL.h>
3 #include <SDL/SDL_opengl.h>
4 #include <stdio.h>
6 //Screen attributes
7 const int SCREEN_WIDTH = 640;
8 const int SCREEN_HEIGHT = 480;
9 const int SCREEN_BPP = 32;
11 //function prototypes
12 void draw_line(int, int);
13 void maneja_eventos();
15 int main(int argc, char *argv[])
17     int quit = 0;
19     //Initialize
20     if( init() == 0 )
21     {
22         return 1;    
23     }    
25     SDL_Surface *screen;
26     SDL_Event event; //The event structure
27         
28     //escucha de eventos    
29     //While the user hasn't quit
30     while( quit == 0 )
31     {
32         //If there's events to handle
33         if( SDL_PollEvent( &event ) )
34         {
35             //The mouse offsets
36             int x = 0, y = 0;
37     
38             //If the mouse moved
39             if( event.type == SDL_MOUSEMOTION )
40             { 
41                 //Get the mouse offsets
42                 x = event.motion.x;
43                 y = event.motion.y;    
44                 draw_line(x,y);
45             }
46     
47             SDL_GL_SwapBuffers(); 
48             //If the user has Xed out the window
49             if( event.type == SDL_QUIT )
50             {
51                 //Quit the program
52                 quit = 1;
53             }    
54         }
55     }   
56     
57     SDL_Quit();
58     
59     return 0;
62 int init_GL()
63 {       
64     //Set clear color
65     glClearColor( 0, 0, 0, 0 );
66     
67     //Set projection
68     glMatrixMode( GL_PROJECTION );
69     glLoadIdentity();
70     glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
71     
72     //Initialize modelview matrix
73     glMatrixMode( GL_MODELVIEW );
74     glLoadIdentity();
75     
76     //If there was any errors
77     if( glGetError() != GL_NO_ERROR )
78     {
79         return 0;    
80     }
81     
82     //If everything initialized
83     return 1;
86 int init()
88     //Initialize SDL
89     if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
90     {
91         return 0;    
92     }
93     
94     //Create Window
95     if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
96     {
97         return 0;
98     }
99     
100     //Initialize OpenGL
101     if( init_GL() == 0 )
102     {
103         return 0;    
104     }
105     
106     //Set caption
107     SDL_WM_SetCaption( "OpenGL Test", NULL );
108     
109     return 1;    
112 void draw_line(int x, int y)
114     // Clear the screen before drawing
115     glClear( GL_COLOR_BUFFER_BIT );
116     
117     float lineWidth = 1.0;
118     // set the line width
119     glLineWidth(lineWidth);
121     // draw the line
122     glBegin(GL_LINES);
123         
124         //eje x
125         glVertex3f( SCREEN_WIDTH/2, 20, 0 );
126         glVertex3f( SCREEN_WIDTH/2,SCREEN_HEIGHT - 20, 0 );
127         //eje y
128         glVertex3f( SCREEN_WIDTH - 20, SCREEN_HEIGHT/2, 0 );
129         glVertex3f( 20, SCREEN_HEIGHT/2,0 );
130         
131         //dibuja lineas
132         glVertex3f( 100, 100, 0 );
133         glVertex3f( x, y, 0 );
134     glEnd();