Posted By

ozkriff on 05/29/09


Tagged


Versions (?)

sdl_opengl_2


 / Published in: C
 

модульность++;

  1. #include "SDL/SDL.h"
  2. #include "SDL/SDL_opengl.h"
  3. #include <math.h>
  4.  
  5. int SCREEN_WIDTH = 640;
  6. int SCREEN_HEIGHT = 480;
  7.  
  8. int QUIT = 0;
  9.  
  10.  
  11. #define DEAD 0
  12. #define NORMAL 1
  13. #define INJURED 2
  14.  
  15. #define PLAYER 0
  16. #define ENEMY 1
  17. #define BULLET 2
  18.  
  19.  
  20. /* -------------------------------< UNIT >-------------------------------- */
  21.  
  22. typedef struct
  23. {
  24. float x, y,
  25. rot_vel, rot,
  26. size,
  27. speed;
  28. short int
  29. type,
  30. state;
  31. struct node
  32. next;
  33. } struct node;
  34.  
  35.  
  36. /* -----------------------------< LIST_ADD >------------------------------ */
  37.  
  38. struct node* list_add (
  39. struct node** p,
  40. const float x,
  41. const float y,
  42. const float rot_vel,
  43. const float rot,
  44. const float size,
  45. const float speed;
  46. const short int type,
  47. const short int state )
  48. {
  49. struct node* n = malloc( sizeof(struct node) );
  50. if (n == NULL) {
  51. puts( "Can't add new item. Something with memory." );
  52. return NULL;
  53. }
  54.  
  55. n->next = *p; /* The previous elm (*p) becomes the "next" element. */
  56. *p = n; /* Add new empty element to the head of the list. */
  57.  
  58. n->x = x;
  59. n->y = y;
  60. n->rot_vel = rot_vel;
  61. n->rot = rot;
  62. n->size = size;
  63. n->speed = speed;
  64. n->type = type;
  65. n->state = state;
  66.  
  67. return *p;
  68. }
  69.  
  70. /* ---------------------------< LIST_REMOVE >----------------------------- */
  71.  
  72. void list_remove ( struct node** p )
  73. {
  74. if (*p != NULL) {
  75. struct node* n = *p;
  76. *p = (*p)->next;
  77. free (n);
  78. }
  79. }
  80.  
  81.  
  82. /* -----------------------------< INIT_GL >------------------------------- */
  83.  
  84. int init_GL ()
  85. {
  86. glClearColor ( 0, 0, 0, 0 );
  87.  
  88. /* Set projection. */
  89. glMatrixMode ( GL_PROJECTION );
  90. glLoadIdentity ();
  91. glOrtho ( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
  92.  
  93. /* Initialize modelview matrix. */
  94. glMatrixMode ( GL_MODELVIEW );
  95. glLoadIdentity ();
  96.  
  97.  
  98. if ( glGetError() != GL_NO_ERROR ) { /* If there was any errors. */
  99. return 1;
  100. }
  101.  
  102. return 0; /* If everything initialized. */
  103. }
  104.  
  105.  
  106. /* -------------------------------< INIT >-------------------------------- */
  107.  
  108. int init ()
  109. {
  110. if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
  111. return 1;
  112. }
  113.  
  114. /* Create Window. */
  115. if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT,
  116. 32, SDL_OPENGL ) == NULL ) {
  117. return 2;
  118. }
  119.  
  120. if ( init_GL() != 0 ) {
  121. return 3;
  122. }
  123.  
  124. SDL_WM_SetCaption( "OpenGL Test", NULL );
  125.  
  126. return 0;
  127. }
  128.  
  129.  
  130. /* ------------------------------< SHOOT >-------------------------------- */
  131.  
  132. int shoot ()
  133. {
  134. return 0;
  135. }
  136.  
  137.  
  138. /* ---------------------------< HANDLE_INPUT >---------------------------- */
  139.  
  140. void handle_input ( SDL_Event event, struct struct node* object )
  141. {
  142. if ( event.type == SDL_KEYDOWN ) /* If a key was pressed. */
  143. switch ( event.key.keysym.sym ) {
  144. case SDLK_w: object->speed = 60; break;
  145. case SDLK_a: object->rot_vel -= 120; break;
  146. case SDLK_s: object->speed = -60; break;
  147. case SDLK_d: object->rot_vel += 120; break;
  148.  
  149. case SDLK_q: QUIT = 1; break;
  150. case SDLK_e: object->shooting= 1; break;
  151.  
  152. default: break;
  153. }
  154. else if ( event.type == SDL_KEYUP ) /* If a key was released. */
  155. switch ( event.key.keysym.sym ) {
  156. case SDLK_w: object->speed = 0; break;
  157. case SDLK_a: object->rot_vel += 120; break;
  158. case SDLK_s: object->speed = 0; break;
  159. case SDLK_d: object->rot_vel -= 120; break;
  160.  
  161. case SDLK_e: object->shooting= 0; break;
  162.  
  163. default: break;
  164. }
  165. }
  166.  
  167.  
  168. /* ---------------------------< MOVE_OBJECT >----------------------------- */
  169.  
  170. void move_object ( Uint32 d_time, struct struct node* object )
  171. {
  172. object->rot += object->rot_vel * ( d_time / 1000.f );
  173.  
  174. object->x += ( object->speed * sinf(object->rot*M_PI/180.0f) )
  175. * (d_time/1000.f);
  176. if ( object->x < 0 ) object->x = 0;
  177. else if ( object->x > SCREEN_WIDTH ) object->x = SCREEN_WIDTH;
  178.  
  179. object->y -= ( object->speed * cosf(object->rot*M_PI/180.0f) )
  180. * (d_time/1000.f);
  181. if ( object->y < 0 ) object->y = 0;
  182. else if ( object->y > SCREEN_HEIGHT ) object->y = SCREEN_HEIGHT;
  183. }
  184.  
  185.  
  186. /* --------------------------< PROCESS_SHOOTS >--------------------------- */
  187.  
  188. void process_shoots ( struct struct node* object, struct struct node* bullet, struct struct node* enemy )
  189. {
  190. if ( object->shooting == 1 ) {
  191. bullet->rot = object->rot;
  192. bullet->x = object->x;
  193. bullet->y = object->y; float x, y,
  194. rot_vel, rot,
  195. size,
  196. speed;
  197. bullet->speed = object->speed + 60;
  198. bullet->live = 1;
  199. }
  200.  
  201. if ( ( enemy->x == bullet->x ) && ( enemy->y == bullet->y ) )
  202. enemy->live = 0;
  203. }
  204.  
  205. /* ---------------------------< DRAW_OBJECT >----------------------------- */
  206.  
  207. void draw_object ( node* object )
  208. {
  209. glTranslatef ( object->x, object->y, 0 );
  210. glRotatef ( object->rot, 0, 0, 1);
  211.  
  212. if ( object->live == 0 )
  213. return;
  214.  
  215. glBegin ( GL_QUADS );
  216. {
  217. if ( object->type == 0 )
  218. glColor4f ( 1.0, 1.0, 1.0, 1.0 );
  219. if ( object->type == 1 ) /* enemy. */
  220. glColor4f ( 1.0, 0.0, 1.0, 1.0 );
  221. else
  222. glColor4f ( 1.0, 0.5, 0.5, 1 );
  223.  
  224. glVertex2f ( -object->size, -object->size );
  225. glVertex2f ( object->size, -object->size );
  226. glVertex2f ( object->size, object->size );
  227. glVertex2f ( -object->size, object->size );
  228. }
  229. glEnd ();
  230.  
  231. glLoadIdentity (); /* Reset. */
  232. }
  233.  
  234.  
  235. /* -------------------------------< MAIN >-------------------------------- */
  236.  
  237. int main ( int argc, char *argv[] )
  238. {
  239.  
  240. SDL_Event event; /* Event handler. */
  241. Uint32 timer = 0,
  242. d_time = 0;
  243. timer = SDL_GetTicks();
  244.  
  245. struct node* enemy = NULL;
  246. struct node* bullet = NULL;
  247. struct node player = {
  248. 100, 100, /* x, y */
  249. 0, 0, /* rot_vel, rot */
  250. 15, /* size */
  251. 0, /* speed */
  252. PLAYER, /* type */
  253. NORMAL, /* state */
  254. NULL /* NEXT */
  255. };
  256. /* - - - - - - - - - - - - - - - - - - - - - - - - - */
  257.  
  258.  
  259.  
  260. /* - - - - - - - - - - - - - - - - - - - - - - - - - */
  261. if ( init() != 0 )
  262. return 1;
  263.  
  264. while ( QUIT == 0 ) {
  265. /* While there are events to handle. */
  266. while ( SDL_PollEvent(&event) ) {
  267. handle_input ( event, &player );
  268. if ( event.type == SDL_QUIT )
  269. QUIT = 1;
  270. }
  271. d_time = SDL_GetTicks () - timer;
  272. move_object ( d_time, & player );
  273. move_object ( d_time, & enemy );
  274. move_object ( d_time, & bullet );
  275.  
  276. process_shoots ( & player, & bullet, & enemy );
  277. timer = SDL_GetTicks ();
  278.  
  279. glClear ( GL_COLOR_BUFFER_BIT ); /* Clear the screen. */
  280. draw_object ( & player );
  281. draw_object ( & enemy );
  282. draw_object ( & bullet );
  283. SDL_GL_SwapBuffers (); /* Update screen. */
  284. }
  285.  
  286. SDL_Quit ();
  287.  
  288. return 0;
  289. }

Report this snippet  

You need to login to post a comment.