diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-10 11:30:41 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-10 11:30:41 -0400 |
commit | 3fde0e7edf310245e5658b767840e742c9341597 (patch) | |
tree | 3fcbf69ecb959bb078078166484608e645f33f33 /src | |
parent | b6ee2b7c296abcfff1ae7f7ac4db976dc4d6fd8e (diff) |
yeah
Diffstat (limited to 'src')
-rw-r--r-- | src/Quest.cpp | 124 | ||||
-rw-r--r-- | src/UIClass.cpp | 44 | ||||
-rw-r--r-- | src/World.cpp | 88 | ||||
-rw-r--r-- | src/main.cpp | 196 |
4 files changed, 239 insertions, 213 deletions
diff --git a/src/Quest.cpp b/src/Quest.cpp index 82c405d..a42e42c 100644 --- a/src/Quest.cpp +++ b/src/Quest.cpp @@ -1,62 +1,62 @@ -#include <Quest.h> - -const Quest QuestList[TOTAL_QUESTS]={ - Quest("Test","A test quest",0) -}; - -Quest::Quest(const char *t,const char *d,unsigned int x){ - size_t len; - title=(char *)malloc((len=strlen(t))); - strncpy(title,t,len); - desc=(char *)malloc((len=strlen(d))); - strncpy(desc,d,len); - xp=x; -} -Quest::~Quest(){ - free(title); - free(desc); - xp=0; -} - -QuestHandler::QuestHandler(){ - ccnt=0; -} -int QuestHandler::assign(const char *t){ - unsigned int i=0; - if(ccnt==QUEST_LIMIT) - return -1; - for(;i<TOTAL_QUESTS;i++){ - if(!strcmp(QuestList[i].title,t)){ - current[ccnt++]=&QuestList[i]; - return ccnt; - } - } - return -1; -} -int QuestHandler::drop(const char *t){ - unsigned char i=0; - for(;i<ccnt;i++){ - if(!strcmp(current[i]->title,t)){ - for(i++;i<ccnt;i++){ - current[i-1]=current[i]; - } - return (--ccnt); - } - } - return -1; -} -int QuestHandler::finish(const char *t){ - unsigned char i=0; - unsigned int j; - for(;i<ccnt;i++){ - if(!strcmp(current[i]->title,t)){ - j=current[i]->xp; - for(i++;i<ccnt;i++){ - current[i-1]=current[i]; - } - ccnt--; - return j; - } - } - return -1; -} +#include <Quest.h>
+
+const Quest QuestList[TOTAL_QUESTS]={
+ Quest("Test","A test quest",0)
+};
+
+Quest::Quest(const char *t,const char *d,unsigned int x){
+ size_t len;
+ title=(char *)malloc((len=strlen(t)));
+ strncpy(title,t,len);
+ desc=(char *)malloc((len=strlen(d)));
+ strncpy(desc,d,len);
+ xp=x;
+}
+Quest::~Quest(){
+ free(title);
+ free(desc);
+ xp=0;
+}
+
+QuestHandler::QuestHandler(){
+ ccnt=0;
+}
+int QuestHandler::assign(const char *t){
+ unsigned int i=0;
+ if(ccnt==QUEST_LIMIT)
+ return -1;
+ for(;i<TOTAL_QUESTS;i++){
+ if(!strcmp(QuestList[i].title,t)){
+ current[ccnt++]=&QuestList[i];
+ return ccnt;
+ }
+ }
+ return -1;
+}
+int QuestHandler::drop(const char *t){
+ unsigned char i=0;
+ for(;i<ccnt;i++){
+ if(!strcmp(current[i]->title,t)){
+ for(i++;i<ccnt;i++){
+ current[i-1]=current[i];
+ }
+ return (--ccnt);
+ }
+ }
+ return -1;
+}
+int QuestHandler::finish(const char *t){
+ unsigned char i=0;
+ unsigned int j;
+ for(;i<ccnt;i++){
+ if(!strcmp(current[i]->title,t)){
+ j=current[i]->xp;
+ for(i++;i<ccnt;i++){
+ current[i-1]=current[i];
+ }
+ ccnt--;
+ return j;
+ }
+ }
+ return -1;
+}
diff --git a/src/UIClass.cpp b/src/UIClass.cpp index 9cdf735..c17b6a7 100644 --- a/src/UIClass.cpp +++ b/src/UIClass.cpp @@ -1,22 +1,22 @@ -#include <UIClass.h> - -void UIClass::handleEvents(){ - SDL_Event e; - while(SDL_PollEvent(&e)){ - switch(e.type){ - case SDL_QUIT: - gameRunning=false; - break; - case SDL_KEYDOWN: - switch(e.key.keysym.sym){ - case 27: - gameRunning=false; - break; - default: - break; - } - default: - break; - } - } -} +#include <UIClass.h>
+
+void UIClass::handleEvents(){
+ SDL_Event e;
+ while(SDL_PollEvent(&e)){
+ switch(e.type){
+ case SDL_QUIT:
+ gameRunning=false;
+ break;
+ case SDL_KEYDOWN:
+ switch(e.key.keysym.sym){
+ case 27:
+ gameRunning=false;
+ break;
+ default:
+ break;
+ }
+ default:
+ break;
+ }
+ }
+}
diff --git a/src/World.cpp b/src/World.cpp index 3d8cfb7..022948f 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1,31 +1,57 @@ -#include <World.h> - -World::World(float width){ - unsigned int i; - lineCount=width/HLINE; - if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){ - std::cout<<"Failed to allocate memory!"<<std::endl; - abort(); - } - line[0].start=(rand()%100)/100.0f-1; // lazy - for(i=1;i<lineCount;i++){ - line[i].start=line[i-1].start+(float)((rand()%20)-10)/1000.0f; - } -} -void World::draw(void){ - unsigned int i; - glBegin(GL_QUADS); - for(i=0;i<lineCount;i++){ - glColor3ub(0,255,0); - glVertex2f((HLINE*i)-1 ,line[i].start); - glVertex2f((HLINE*i)-1+HLINE,line[i].start); - glVertex2f((HLINE*i)-1+HLINE,line[i].start-0.02); - glVertex2f((HLINE*i)-1 ,line[i].start-0.02); - glColor3ub(150,100,50); - glVertex2f((HLINE*i)-1 ,line[i].start-0.02); - glVertex2f((HLINE*i)-1+HLINE,line[i].start-0.02); - glVertex2f((HLINE*i)-1+HLINE,-1); - glVertex2f((HLINE*i)-1 ,-1); - } - glEnd(); -} +#include <World.h>
+
+World::World(float width){
+ unsigned int i;
+ double f;
+ lineCount=width/HLINE+1;
+ if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){
+ std::cout<<"Failed to allocate memory!"<<std::endl;
+ abort();
+ }
+ line[0].start=(rand()%100)/100.0f-0.8f; // lazy
+ if(line[0].start>-0.5f)line[0].start=-0.7f;
+ for(i=10;i<lineCount;i+=10){
+ line[i].start=((double)(rand()%40+200))/1000.0f-1;
+ }
+ for(i=0;i<lineCount;i++){
+ if(!(i%10)||!i){
+ f=line[i+10].start-line[i].start;
+ f/=10.0f;
+ }else{
+ line[i].start=line[i-1].start+f;
+ }
+ }
+}
+void World::draw(void){
+ unsigned int i;
+ glBegin(GL_QUADS);
+ for(i=0;i<lineCount;i++){
+ glColor3ub(0,255,0);
+ glVertex2f((HLINE*i)-1 ,line[i].start);
+ glVertex2f((HLINE*i)-1+HLINE,line[i].start);
+ glVertex2f((HLINE*i)-1+HLINE,line[i].start-HLINE*2);
+ glVertex2f((HLINE*i)-1 ,line[i].start-HLINE*2);
+ glColor3ub(150,100,50);
+ glVertex2f((HLINE*i)-1 ,line[i].start-HLINE*2);
+ glVertex2f((HLINE*i)-1+HLINE,line[i].start-HLINE*2);
+ glVertex2f((HLINE*i)-1+HLINE,-1);
+ glVertex2f((HLINE*i)-1 ,-1);
+ }
+ glEnd();
+}
+void World::detect(vec2 *v,const float width){
+ unsigned int i;
+ for(i=0;i<lineCount;i++){
+ if(v->y<line[i].start){
+ if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){
+ v->x=(HLINE*i)-1+HLINE;
+ }else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){
+ v->x=(HLINE*i)-1-width;
+ }else{
+ v->y=line[i].start;
+ }
+ }else if(v->y>line[i].start+HLINE/2){
+ v->y-=HLINE/4;
+ }
+ }
+}
diff --git a/src/main.cpp b/src/main.cpp index cb93ff4..b04cb59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,98 +1,98 @@ -#include <common.h> -#include <ctime> - -SDL_Window *window = NULL; -SDL_Surface *renderSurface = NULL; -SDL_GLContext mainGLContext = NULL; - -bool gameRunning = true; - -UIClass ui; - -int main(int argc,char **argv){ - //runs start-up procedures - if(!SDL_Init(SDL_INIT_VIDEO)){ - atexit(SDL_Quit); - if(!(IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG)&(IMG_INIT_PNG|IMG_INIT_JPG))){ - std::cout<<"Could not init image libraries!\n"<<std::endl; - return -1; - } - atexit(IMG_Quit); - //Turn on double Buffering - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - //create the window - window = SDL_CreateWindow("Independent Study v.0.1 alpha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL - #ifdef FULLSCREEN - | SDL_WINDOW_FULLSCREEN - #endif // FULLSCREEN - ); - if(window){ - //set OpenGL context - mainGLContext = SDL_GL_CreateContext(window); - if(mainGLContext == NULL){ - std::cout << "The OpenGL context failed to initialize! Error: " << SDL_GetError() << std::endl; - } - }else{ - std::cout << "The window failed to generate! Error: " << SDL_GetError() << std::endl; - return -1; - } - }else{ - std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl; - return -1; - } - srand(time(NULL)); - glClearColor(.3,.5,.8,0); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); - - /************************** - **** GAMELOOP **** - **************************/ - - World *w=new World(2); - - while(gameRunning){ - ui.handleEvents(); // Handle events - //a matrix is a blank canvas for the computer to draw on, the matrices are stored in a "stack" - //GL_PROJECTION has 2 matrices - //GL_MODELVIEW has 32 matrices - glMatrixMode(GL_PROJECTION); //set the matrix mode as projection so we can set the ortho size and the camera settings later on - glPushMatrix(); //push the matrix to the top of the matrix stack - glLoadIdentity(); //replace the entire matrix stack with the updated GL_PROJECTION mode - //glOrtho(0,SCREEN_WIDTH, 0,SCREEN_HEIGHT, -1,1); //set the the size of the screen - glMatrixMode(GL_MODELVIEW); //set the matrix to modelview so we can draw objects - glPushMatrix(); //push the matrix to the top of the matrix stack - glLoadIdentity(); //replace the entire matrix stack with the updated GL_MODELVIEW mode - glPushMatrix(); //basically here we put a blank canvas (new matrix) on the screen to draw on - glClear(GL_COLOR_BUFFER_BIT); //clear the matrix on the top of the stack - - /************************** - **** RENDER STUFF HERE **** - **************************/ - - /*glColor3f(1.0f, 0.0f, 0.0f); //color to red - glRectf(0,0, 50,50); //draw a test rectangle - glColor3f(0.0f, 1.0f, 0.0f); //color to blue - glRectf(50,0, 100,50); //draw a test rectangle - glColor3f(0.0f, 0.0f, 1.0f); //color to green - glRectf(100,0,150,50); //draw a test rectangle*/ - - w->draw(); - - /************************** - **** CLOSE THE LOOP **** - **************************/ - - glPopMatrix(); //take the matrix(s) off the stack to pass them to the renderer - SDL_GL_SwapWindow(window); //give the stack to SDL to render it - } - - /************************** - **** CLOSE PROGRAM **** - **************************/ - - //closes the window and frees resources - SDL_GL_DeleteContext(mainGLContext); - SDL_DestroyWindow(window); - return 0; -} +#include <common.h>
+#include <ctime>
+
+SDL_Window *window = NULL;
+SDL_Surface *renderSurface = NULL;
+SDL_GLContext mainGLContext = NULL;
+
+bool gameRunning = true;
+
+UIClass ui;
+Entities *entit1;
+Player player;
+
+int main(int argc,char **argv){
+ // Initialize SDL
+ if(!SDL_Init(SDL_INIT_VIDEO)){
+ atexit(SDL_Quit);
+ }else{
+ std::cout << "SDL was not able to initialize! Error: " << SDL_GetError() << std::endl;
+ return -1;
+ }
+ // Initialize SDL_image
+ if((IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG)&(IMG_INIT_PNG|IMG_INIT_JPG))){
+ atexit(IMG_Quit);
+ }else{
+ std::cout<<"Could not init image libraries!\n"<<std::endl;
+ return -1;
+ }
+ // Create the window
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+ window = SDL_CreateWindow("Independent Study v.0.1 alpha", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
+ #ifdef FULLSCREEN
+ | SDL_WINDOW_FULLSCREEN
+ #endif // FULLSCREEN
+ );
+ if(!window){
+ std::cout << "The window failed to generate! Error: " << SDL_GetError() << std::endl;
+ return -1;
+ }
+ // Set OpenGL context
+ if((mainGLContext = SDL_GL_CreateContext(window))==NULL){
+ std::cout << "The OpenGL context failed to initialize! Error: " << SDL_GetError() << std::endl;
+ }
+ // Setup rand() and OpenGL
+ srand(time(NULL));
+ glClearColor(.3,.5,.8,0);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+
+ /**************************
+ **** GAMELOOP ****
+ **************************/
+
+ entit1 = &player;
+ entit1->spawn(0,0);
+
+ World *w=new World(2);
+
+ while(gameRunning){
+ ui.handleEvents(); // Handle events
+ //a matrix is a blank canvas for the computer to draw on, the matrices are stored in a "stack"
+ //GL_PROJECTION has 2 matrices
+ //GL_MODELVIEW has 32 matrices
+ glMatrixMode(GL_PROJECTION); //set the matrix mode as projection so we can set the ortho size and the camera settings later on
+ glPushMatrix(); //push the matrix to the top of the matrix stack
+ glLoadIdentity(); //replace the entire matrix stack with the updated GL_PROJECTION mode
+ glOrtho(-1,1,-1,1,-1,1); //set the the size of the screen
+ glMatrixMode(GL_MODELVIEW); //set the matrix to modelview so we can draw objects
+ glPushMatrix(); //push the matrix to the top of the matrix stack
+ glLoadIdentity(); //replace the entire matrix stack with the updated GL_MODELVIEW mode
+ glPushMatrix(); //basically here we put a blank canvas (new matrix) on the screen to draw on
+ glClear(GL_COLOR_BUFFER_BIT); //clear the matrix on the top of the stack
+
+ /**************************
+ **** RENDER STUFF HERE ****
+ **************************/
+
+ w->draw();
+ glColor3ub(0,0,0);
+ glRectf(player.loc.x, player.loc.y, player.loc.x + player.width, player.loc.y + player.height);
+
+ /**************************
+ **** CLOSE THE LOOP ****
+ **************************/
+
+ glPopMatrix(); //take the matrix(s) off the stack to pass them to the renderer
+ SDL_GL_SwapWindow(window); //give the stack to SDL to render it
+ }
+
+ /**************************
+ **** CLOSE PROGRAM ****
+ **************************/
+
+ //closes the window and frees resources
+ SDL_GL_DeleteContext(mainGLContext);
+ SDL_DestroyWindow(window);
+ return 0;
+}
|