/ Published in: C
модульность++;
Expand |
Embed | Plain Text
#include "SDL/SDL.h" #include "SDL/SDL_opengl.h" #include <math.h> int SCREEN_WIDTH = 640; int SCREEN_HEIGHT = 480; int QUIT = 0; #define DEAD 0 #define NORMAL 1 #define INJURED 2 #define PLAYER 0 #define ENEMY 1 #define BULLET 2 /* -------------------------------< UNIT >-------------------------------- */ typedef struct { float x, y, rot_vel, rot, size, speed; short int type, state; struct node next; } struct node; /* -----------------------------< LIST_ADD >------------------------------ */ struct node* list_add ( struct node** p, const float x, const float y, const float rot_vel, const float rot, const float size, const float speed; const short int type, const short int state ) { struct node* n = malloc( sizeof(struct node) ); if (n == NULL) { puts( "Can't add new item. Something with memory." ); return NULL; } n->next = *p; /* The previous elm (*p) becomes the "next" element. */ *p = n; /* Add new empty element to the head of the list. */ n->x = x; n->y = y; n->rot_vel = rot_vel; n->rot = rot; n->size = size; n->speed = speed; n->type = type; n->state = state; return *p; } /* ---------------------------< LIST_REMOVE >----------------------------- */ void list_remove ( struct node** p ) { if (*p != NULL) { struct node* n = *p; *p = (*p)->next; free (n); } } /* -----------------------------< INIT_GL >------------------------------- */ int init_GL () { glClearColor ( 0, 0, 0, 0 ); /* Set projection. */ glMatrixMode ( GL_PROJECTION ); glLoadIdentity (); glOrtho ( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 ); /* Initialize modelview matrix. */ glMatrixMode ( GL_MODELVIEW ); glLoadIdentity (); if ( glGetError() != GL_NO_ERROR ) { /* If there was any errors. */ return 1; } return 0; /* If everything initialized. */ } /* -------------------------------< INIT >-------------------------------- */ int init () { if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { return 1; } /* Create Window. */ if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_OPENGL ) == NULL ) { return 2; } if ( init_GL() != 0 ) { return 3; } SDL_WM_SetCaption( "OpenGL Test", NULL ); return 0; } /* ------------------------------< SHOOT >-------------------------------- */ int shoot () { return 0; } /* ---------------------------< HANDLE_INPUT >---------------------------- */ void handle_input ( SDL_Event event, struct struct node* object ) { if ( event.type == SDL_KEYDOWN ) /* If a key was pressed. */ switch ( event.key.keysym.sym ) { case SDLK_w: object->speed = 60; break; case SDLK_a: object->rot_vel -= 120; break; case SDLK_s: object->speed = -60; break; case SDLK_d: object->rot_vel += 120; break; case SDLK_q: QUIT = 1; break; case SDLK_e: object->shooting= 1; break; default: break; } else if ( event.type == SDL_KEYUP ) /* If a key was released. */ switch ( event.key.keysym.sym ) { case SDLK_w: object->speed = 0; break; case SDLK_a: object->rot_vel += 120; break; case SDLK_s: object->speed = 0; break; case SDLK_d: object->rot_vel -= 120; break; case SDLK_e: object->shooting= 0; break; default: break; } } /* ---------------------------< MOVE_OBJECT >----------------------------- */ void move_object ( Uint32 d_time, struct struct node* object ) { object->rot += object->rot_vel * ( d_time / 1000.f ); object->x += ( object->speed * sinf(object->rot*M_PI/180.0f) ) * (d_time/1000.f); if ( object->x < 0 ) object->x = 0; else if ( object->x > SCREEN_WIDTH ) object->x = SCREEN_WIDTH; object->y -= ( object->speed * cosf(object->rot*M_PI/180.0f) ) * (d_time/1000.f); if ( object->y < 0 ) object->y = 0; else if ( object->y > SCREEN_HEIGHT ) object->y = SCREEN_HEIGHT; } /* --------------------------< PROCESS_SHOOTS >--------------------------- */ void process_shoots ( struct struct node* object, struct struct node* bullet, struct struct node* enemy ) { if ( object->shooting == 1 ) { bullet->rot = object->rot; bullet->x = object->x; bullet->y = object->y; float x, y, rot_vel, rot, size, speed; bullet->speed = object->speed + 60; bullet->live = 1; } if ( ( enemy->x == bullet->x ) && ( enemy->y == bullet->y ) ) enemy->live = 0; } /* ---------------------------< DRAW_OBJECT >----------------------------- */ void draw_object ( node* object ) { glTranslatef ( object->x, object->y, 0 ); glRotatef ( object->rot, 0, 0, 1); if ( object->live == 0 ) return; glBegin ( GL_QUADS ); { if ( object->type == 0 ) glColor4f ( 1.0, 1.0, 1.0, 1.0 ); if ( object->type == 1 ) /* enemy. */ glColor4f ( 1.0, 0.0, 1.0, 1.0 ); else glColor4f ( 1.0, 0.5, 0.5, 1 ); glVertex2f ( -object->size, -object->size ); glVertex2f ( object->size, -object->size ); glVertex2f ( object->size, object->size ); glVertex2f ( -object->size, object->size ); } glEnd (); glLoadIdentity (); /* Reset. */ } /* -------------------------------< MAIN >-------------------------------- */ int main ( int argc, char *argv[] ) { SDL_Event event; /* Event handler. */ Uint32 timer = 0, d_time = 0; timer = SDL_GetTicks(); struct node* enemy = NULL; struct node* bullet = NULL; struct node player = { 100, 100, /* x, y */ 0, 0, /* rot_vel, rot */ 15, /* size */ 0, /* speed */ PLAYER, /* type */ NORMAL, /* state */ NULL /* NEXT */ }; /* - - - - - - - - - - - - - - - - - - - - - - - - - */ /* - - - - - - - - - - - - - - - - - - - - - - - - - */ if ( init() != 0 ) return 1; while ( QUIT == 0 ) { /* While there are events to handle. */ while ( SDL_PollEvent(&event) ) { handle_input ( event, &player ); if ( event.type == SDL_QUIT ) QUIT = 1; } d_time = SDL_GetTicks () - timer; move_object ( d_time, & player ); move_object ( d_time, & enemy ); move_object ( d_time, & bullet ); process_shoots ( & player, & bullet, & enemy ); timer = SDL_GetTicks (); glClear ( GL_COLOR_BUFFER_BIT ); /* Clear the screen. */ draw_object ( & player ); draw_object ( & enemy ); draw_object ( & bullet ); SDL_GL_SwapBuffers (); /* Update screen. */ } SDL_Quit (); return 0; }
You need to login to post a comment.
