aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changelog7
-rw-r--r--include/Texture.h4
-rw-r--r--include/common.h22
-rw-r--r--include/entities.h3
-rw-r--r--include/inventory.h14
-rw-r--r--include/world.h17
-rw-r--r--main.cpp4
-rw-r--r--src/Quest.cpp8
-rw-r--r--src/Texture.cpp12
-rw-r--r--src/common.cpp26
-rw-r--r--src/entities.cpp30
-rw-r--r--src/gameplay.cpp24
-rw-r--r--src/inventory.cpp30
-rw-r--r--src/ui.cpp30
-rw-r--r--src/world.cpp52
15 files changed, 102 insertions, 181 deletions
diff --git a/Changelog b/Changelog
index 0444f3d..c9c94d8 100644
--- a/Changelog
+++ b/Changelog
@@ -338,3 +338,10 @@
- Bug fixes related to memory allocation
- Broke item textures...
- Finshed new inventory ui animations
+
+11/30/2015:
+===========
+
+ - Converted all m/calloc/free calls to new/delete
+ - fixed hardcoded string issues
+ - improved inventory animation
diff --git a/include/Texture.h b/include/Texture.h
index a3f242b..c590f6a 100644
--- a/include/Texture.h
+++ b/include/Texture.h
@@ -11,12 +11,12 @@ namespace Texture{
class Texturec{
private:
- int texState;
+ unsigned int texState;
public:
Texturec(uint amt, ...);
void bindNext();
void bindPrev();
- void bind(int);
+ void bind(unsigned int);
void walk();
GLuint *image;
diff --git a/include/common.h b/include/common.h
index 525a4cd..0ddb5e4 100644
--- a/include/common.h
+++ b/include/common.h
@@ -9,8 +9,7 @@
#include <cstdlib>
#include <vector>
#include <math.h>
- #include <thread>
-
+#include <thread>
/*
* Include GLEW and the SDL 2 headers
@@ -60,10 +59,10 @@ typedef struct {
* and whether or not we want the window to be fullscreen.
*/
-#define GAME_NAME "Independent Study v.0.3 alpha"
+#define GAME_NAME "Independent Study v.0.4 alpha"
-#define SCREEN_WIDTH 1792
-#define SCREEN_HEIGHT 1008
+#define SCREEN_WIDTH 800
+#define SCREEN_HEIGHT 600
//#define FULLSCREEN
@@ -118,14 +117,6 @@ extern vec2 offset;
extern float handAngle;
/*
- * Loads an image from the given file path and attempts to make a texture out of it. The
- * resulting GLuint is returned (used to recall the texture in glBindTexture).
- *
-*/
-
-//GLuint loadTexture(const char *fileName);
-
-/*
* Prints a formatted debug message to the console, along with the callee's file and line
* number.
*
@@ -136,9 +127,4 @@ void DEBUG_prints(const char* file, int line, const char *s,...);
void safeSetColor(int r,int g,int b);
void safeSetColorA(int r,int g,int b,int a);
-unsigned int safe_strlen(const char*);
-
-void DrawCircle(float cx, float cy, float r, int num_segments);
-
-
#endif // COMMON_H
diff --git a/include/entities.h b/include/entities.h
index be5c000..b9881ea 100644
--- a/include/entities.h
+++ b/include/entities.h
@@ -112,8 +112,7 @@ public:
class Object : public Entity{
public:
- Object(int);
- Object(int, bool, char*);
+ Object(ITEM_ID id, bool qo, const char *pd);
void interact(void);
bool questObject = false;
char *pickupDialog;
diff --git a/include/inventory.h b/include/inventory.h
index 2793ad7..a9a4bcb 100644
--- a/include/inventory.h
+++ b/include/inventory.h
@@ -50,18 +50,7 @@ public:
char* textureLoc;
Texturec *tex;
int count;
- Item(ITEM_ID i, char* n, ITEM_TYPE t, float w, float h, int m, char* tl):
- id(i), type(t), width(w), height(h), maxStackSize(m){
- count = 0;
-
- name = (char*)calloc(strlen(n ),sizeof(char));
- textureLoc = (char*)calloc(strlen(tl),sizeof(char));
-
- strcpy(name,n);
- strcpy(textureLoc,tl);
-
- tex= new Texturec(1,textureLoc);
- }
+ Item(ITEM_ID i, const char *n, ITEM_TYPE t, float w, float h, int m, const char *tl);
GLuint rtex(){
return tex->image[0];
}
@@ -101,5 +90,6 @@ public:
};
void itemUse(void *p);
+char *getItemTexturePath(ITEM_ID id);
#endif // INVENTORY_H
diff --git a/include/world.h b/include/world.h
index 4c82b2c..a2414e6 100644
--- a/include/world.h
+++ b/include/world.h
@@ -1,7 +1,7 @@
#ifndef WORLD_H
#define WORLD_H
-#include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
+#include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
#include <entities.h>
#define GEN_INC 10 // Defines at what interval y values should be calculated for the array 'line'.
@@ -102,7 +102,7 @@ public:
void addMob(int t,float x,float y);
void addMob(int t,float x,float y,void (*hey)());
void addNPC(float x,float y);
- void addObject(int, bool, char*, float, float);
+ void addObject(ITEM_ID, bool, const char *, float, float);
void update(Player *p,unsigned int delta);
@@ -176,19 +176,6 @@ public:
int getTheWidth(void);
- /*
- * Stores all of this class's contents in a string for saving to a file. This array should
- * be freed after being written to a file.
- */
-
- char *save(unsigned int *ssize);
-
- /*
- * Loads a previous world's contents (from save()) into this one.
- */
-
- void load(char *buf);
-
};
/*
diff --git a/main.cpp b/main.cpp
index 30ee463..5ac85ba 100644
--- a/main.cpp
+++ b/main.cpp
@@ -496,11 +496,11 @@ void mainLoop(void){
/*
* Run the logic handler if MSEC_PER_TICK milliseconds have passed.
*/
+
if(prevPrevTime + MSEC_PER_TICK >= currentTime){
logic();
prevPrevTime = currentTime;
}
- //ui::handleMouse();
/*
* Update player and entity coordinates.
@@ -519,7 +519,7 @@ void mainLoop(void){
}else if(!(debugDiv%10)){
debugY = player->loc.y;
- }
+ }
render(); // Call the render loop
diff --git a/src/Quest.cpp b/src/Quest.cpp
index 6789a05..bfa8966 100644
--- a/src/Quest.cpp
+++ b/src/Quest.cpp
@@ -16,16 +16,16 @@ const Quest QuestList[TOTAL_QUESTS]={
Quest::Quest(const char *t,const char *d,struct item_t r){
- title=(char *)calloc(safe_strlen(t),sizeof(char));
- desc=(char *)calloc(safe_strlen(d),sizeof(char));
+ title = new char[strlen(t)+1]; //(char *)calloc(safe_strlen(t),sizeof(char));
+ desc = new char[strlen(d)+1]; //(char *)calloc(safe_strlen(d),sizeof(char));
strcpy(title,t);
strcpy(desc,d);
memcpy(&reward,&r,sizeof(struct item_t));
}
Quest::~Quest(){
- free(title);
- free(desc);
+ delete[] title; //free(title);
+ delete[] desc; //free(desc);
memset(&reward,0,sizeof(struct item_t));
}
diff --git a/src/Texture.cpp b/src/Texture.cpp
index 82baa71..8b40513 100644
--- a/src/Texture.cpp
+++ b/src/Texture.cpp
@@ -53,8 +53,8 @@ namespace Texture{
SDL_FreeSurface(image); // Free the surface
- LoadedTexture[LoadedTextureCounter] = (struct texture_t *)malloc(sizeof(struct texture_t));
- LoadedTexture[LoadedTextureCounter]->name = (char *)malloc(safe_strlen(fileName));
+ LoadedTexture[LoadedTextureCounter] = new struct texture_t; //(struct texture_t *)malloc(sizeof(struct texture_t));
+ LoadedTexture[LoadedTextureCounter]->name = new char[strlen(fileName)+1]; //(char *)malloc(safe_strlen(fileName));
LoadedTexture[LoadedTextureCounter]->tex = object;
strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName);
LoadedTextureCounter++;
@@ -64,17 +64,17 @@ namespace Texture{
}
Texturec::Texturec(uint amt, ...){
- image = new GLuint(amt);
+ texState = 0;
+ image = new GLuint[amt];
va_list fNames;
va_start(fNames, amt);
for(int i = 0; i < amt; i++){
- char* f = va_arg(fNames, char*);
- image[i] = Texture::loadTexture(f);
+ image[i] = Texture::loadTexture(va_arg(fNames, char *));
}
va_end(fNames);
}
-void Texturec::bind(int bn){
+void Texturec::bind(unsigned int bn){
texState = bn;
glBindTexture(GL_TEXTURE_2D, image[texState]);
}
diff --git a/src/common.cpp b/src/common.cpp
index f3c3999..a8a964e 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -32,29 +32,3 @@ void safeSetColorA(int r,int g,int b,int a){
if(a<0)a=0;
glColor4ub(r,g,b,a);
}
-
-//only trust the NSA
-#define STRLEN_MIN 32
-
-unsigned int safe_strlen(const char *s){
- unsigned int size=0;
- while(s[size])size++;
- if(size<STRLEN_MIN)return STRLEN_MIN;
- else return size;
-}
-
-void DrawCircle(float cx, float cy, float r, int num_segments)
-{
- glBegin(GL_LINE_LOOP);
- for(int ii = 0; ii < num_segments; ii++)
- {
- float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle
-
- float x = r * cosf(theta);//calculate the x component
- float y = r * sinf(theta);//calculate the y component
-
- glVertex2f(x + cx, y + cy);//output vertex
-
- }
- glEnd();
-} \ No newline at end of file
diff --git a/src/entities.cpp b/src/entities.cpp
index 97b4d35..4e582c9 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -33,7 +33,7 @@ void Entity::spawn(float x, float y){ //spawns the entity you pass to it based o
}
}
- name = (char*)malloc(16);
+ name = new char[16]; //(char*)malloc(16);
getName();
}
@@ -100,21 +100,13 @@ Mob::Mob(int sub){
inv = new Inventory(NPC_INV_SIZE);
}
-Object::Object(int id):identifier(id){
- type = OBJECTT;
- alive = true;
- near = false;
- width = HLINE * 8;
- height = HLINE * 8;
-
- maxHealth = health = 1;
- //tex = new Texturec(1, item[id].textureLoc);
-
- questObject = false;
- pickupDialog="\0";
-}
-
-Object::Object(int id, bool qo, char *pd):identifier(id),questObject(qo),pickupDialog(pd){
+Object::Object(ITEM_ID id, bool qo, const char *pd){
+ identifier = id;
+ questObject = qo;
+
+ pickupDialog = new char[strlen(pd)+1];
+ strcpy(pickupDialog,pd);
+
type = OBJECTT;
alive = true;
near = false;
@@ -122,7 +114,7 @@ Object::Object(int id, bool qo, char *pd):identifier(id),questObject(qo),pickupD
height = HLINE * 8;
maxHealth = health = 1;
- //tex = new Texturec(1, item[id].textureLoc);
+ tex = new Texturec(1,getItemTexturePath(id));
}
@@ -218,7 +210,7 @@ NOPE:
void Entity::getName(){
rewind(names);
- char buf,*bufs = (char *)malloc(16);
+ char buf,*bufs = new char[16]; //(char *)malloc(16);
int tempNum,max = 0;
for(;!feof(names);max++){
fgets(bufs,16,(FILE*)names);
@@ -245,7 +237,7 @@ void Entity::getName(){
bufs[strlen(bufs)-1] = '\0';
strcpy(name,bufs);
}
- free(bufs);
+ delete[] bufs; //free(bufs);
}
void Player::interact(){ //the function that will cause the player to search for things to interact with
diff --git a/src/gameplay.cpp b/src/gameplay.cpp
index 6e85b6d..2fd7424 100644
--- a/src/gameplay.cpp
+++ b/src/gameplay.cpp
@@ -97,28 +97,12 @@ void initEverything(void){
/*
* World creation:
*/
+
World *test=new World();
World *playerSpawnHill=new World();
- /*
- * Load the saved world if it exists, otherwise generate a new one.
- */
-
- /*FILE *worldLoad;
- if((worldLoad=fopen("world.dat","r"))){
- std::cout<<"Yes"<<std::endl;
- char *buf;
- unsigned int size;
- fseek(worldLoad,0,SEEK_END);
- size=ftell(worldLoad);
- rewind(worldLoad);
- buf=(char *)malloc(size);
- fread(buf,1,size,worldLoad);
- test->load(buf);
- }else{*/
- test->generate(SCREEN_WIDTH*2);
- test->addHole(100,150);
- //}
+ test->generate(SCREEN_WIDTH*2);
+ test->addHole(100,150);
test->setBackground(BG_FOREST);
test->addLayer(400);
@@ -160,7 +144,7 @@ void initEverything(void){
test->addMob(MS_RABBIT,200,100);
test->addMob(MS_BIRD,-500,500);
- currentWorld->addObject(SWORD_WOOD, false, NULL, 500,200);
+ currentWorld->addObject(SWORD_WOOD, false, "", 500,200);
currentWorld->addObject(FLASHLIGHT, true, "This looks important, do you want to pick it up?",600,200);
/*currentWorld->addObject(DEBUG_ITEM, 500,200);
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 45d5bce..cd01c11 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -13,15 +13,38 @@ static Item item[5]= {
void itemDraw(Player *p,ITEM_ID id);
+char *getItemTexturePath(ITEM_ID id){
+ return item[id].textureLoc;
+}
+
+Item::Item(ITEM_ID i, const char *n, ITEM_TYPE t, float w, float h, int m, const char *tl){
+ id = i;
+ type = t;
+ width = w;
+ height = h;
+ maxStackSize = m;
+ count = 0;
+
+ name = new char[strlen(n)+1]; //(char*)calloc(strlen(n ),sizeof(char));
+ textureLoc = new char[strlen(tl)+1]; //(char*)calloc(strlen(tl),sizeof(char));
+
+ strcpy(name,n);
+ strcpy(textureLoc,tl);
+
+ tex= new Texturec(1,textureLoc);
+}
+
Inventory::Inventory(unsigned int s){
sel=0;
size=s;
- inv=(struct item_t *)calloc(size,sizeof(struct item_t));
+ inv = new struct item_t[size]; //(struct item_t *)calloc(size,sizeof(struct item_t));
+ memset(inv,0,size*sizeof(struct item_t));
tossd=false;
}
Inventory::~Inventory(void){
- free(item);
+ delete[] inv;
+ //free(item);
}
void Inventory::setSelection(unsigned int s){
@@ -64,8 +87,6 @@ void Inventory::draw(void){
ui::putText(offset.x-SCREEN_WIDTH/2,480,"%d",sel);
unsigned int i=0;
float y,xoff;
-
-
static int numSlot = 7;
static std::vector<int>dfp(numSlot);
static std::vector<Ray>iray(numSlot);
@@ -158,6 +179,7 @@ void Inventory::draw(void){
i++;
}
}*/
+
if(inv[sel].count)itemDraw(player,inv[sel].id);
}
diff --git a/src/ui.cpp b/src/ui.cpp
index 790248b..324192d 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -132,7 +132,7 @@ namespace ui {
* Pre-render 'all' the characters.
*/
- glDeleteTextures(93,ftex); // Delete any already-rendered textures
+ glDeleteTextures(93,ftex); // delete[] any already-rendered textures
glGenTextures(93,ftex); // Generate new texture name/locations?
for(i=33;i<126;i++){
@@ -163,7 +163,7 @@ namespace ui {
* making it white-on-black.
*/
- buf=(char *)malloc(ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows*4);
+ buf = new char[ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4]; //(char *)malloc(ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows*4);
for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){
buf[j*4 ]=fontColor[0];
@@ -181,7 +181,7 @@ namespace ui {
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
- free(buf);
+ delete[] buf; //free(buf);
}
}
@@ -302,7 +302,10 @@ namespace ui {
* Create a well-sized buffer if we haven't yet.
*/
- if(!ret) ret=(char *)calloc(512,sizeof(char));
+ if(!ret){
+ ret = new char[512]; //(char *)calloc(512,sizeof(char));
+ memset(ret,0,512*sizeof(char));
+ }
/*
* Reset values if a new string is being passed.
@@ -343,7 +346,8 @@ namespace ui {
* Create a wimpy buffer.
*/
- buf=(char *)calloc(128,sizeof(char));
+ buf = new char[128]; //(char *)calloc(128,sizeof(char));
+ memset(buf,0,128*sizeof(char));
/*
* Handle the formatted string, printing it to the buffer.
@@ -358,7 +362,7 @@ namespace ui {
*/
width=putString(x,y,buf);
- free(buf);
+ delete[] buf; //free(buf);
return width;
}
@@ -372,7 +376,7 @@ namespace ui {
* Set up the text buffer.
*/
- if(!dialogBoxText) dialogBoxText=(char *)malloc(512);
+ if(!dialogBoxText) dialogBoxText = new char[512]; //(char *)malloc(512);
memset(dialogBoxText,0,512);
/*
@@ -394,7 +398,7 @@ namespace ui {
while(dialogOptCount){
if(dialogOptText[dialogOptCount])
- free(dialogOptText[dialogOptCount]);
+ delete[] dialogOptText[dialogOptCount]; //free(dialogOptText[dialogOptCount]);
dialogOptCount--;
};
dialogOptChosen=0;
@@ -402,7 +406,7 @@ namespace ui {
sopt=strtok(opt,":");
while(sopt != NULL){
- dialogOptText[dialogOptCount]=(char *)malloc(strlen(sopt));
+ dialogOptText[dialogOptCount] = new char[strlen(sopt)+1]; //(char *)malloc(strlen(sopt));
strcpy(dialogOptText[dialogOptCount++],sopt);
sopt=strtok(NULL,":");
}
@@ -416,19 +420,19 @@ namespace ui {
}
void importantText(const char *text,...){
va_list textArgs;
- char *ttext;
+ char *ttext,*rtext;
if(!player->ground)return;
va_start(textArgs,text);
- ttext=(char *)calloc(512,sizeof(char));
+ ttext = new char[512]; //(char *)calloc(512,sizeof(char));
+ memset(ttext,0,512*sizeof(char));
vsnprintf(ttext,512,text,textArgs);
va_end(textArgs);
setFontSize(24);
- char *rtext;
rtext=typeOut(ttext);
putString(offset.x-SCREEN_WIDTH/2,
offset.y+fontSize,
rtext);
- free(ttext);
+ delete[] ttext; //free(ttext);
}
void draw(void){
unsigned char i;
diff --git a/src/world.cpp b/src/world.cpp
index 6f31d6d..c977c7f 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -43,34 +43,6 @@ float worldGetYBase(World *w){
return /*base*/ GEN_MIN;
}
-struct wSavePack {
- int x_start;
- unsigned int lineCount;
-} __attribute__ ((packed));
-
-char *World::save(unsigned int *ssize){
- struct wSavePack *sp;
- unsigned int size;
- char *buf;
- size=sizeof(struct wSavePack) + lineCount * sizeof(struct line_t);
- buf=(char *)malloc(size);
- sp=(struct wSavePack *)buf;
- sp->x_start=x_start;
- sp->lineCount=lineCount;
- memcpy(buf+sizeof(struct wSavePack),line,lineCount * sizeof(struct line_t));
- *ssize=size;
- return buf;
-}
-
-void World::load(char *buf){
- struct wSavePack *sp;
- sp=(struct wSavePack *)buf;
- x_start=sp->x_start;
- lineCount=sp->lineCount;
- line=(struct line_t *)calloc(lineCount,sizeof(struct line_t));
- memcpy(line,buf+sizeof(struct wSavePack),lineCount * sizeof(struct line_t));
-}
-
void World::setBackground(WORLD_BG_TYPE bgt){
switch(bgt){
default:
@@ -95,7 +67,8 @@ World::World(void){
toLeft =
toRight = NULL;
- star = (vec2 *)calloc(100,sizeof(vec2));
+ star = new vec2[100]; //(vec2 *)calloc(100,sizeof(vec2));
+ memset(star,0,100*sizeof(vec2));
}
void World::generate(unsigned int width){ // Generates the world and sets all variables contained in the World class.
@@ -118,7 +91,8 @@ void World::generate(unsigned int width){ // Generates the world and sets all va
* Allocate enough memory for the world to be stored.
*/
- line=(struct line_t *)calloc(lineCount,sizeof(struct line_t));
+ line = new struct line_t[lineCount]; //(struct line_t *)calloc(lineCount,sizeof(struct line_t));
+ memset(line,0,lineCount*sizeof(struct line_t));
/*
* Set an initial y to base generation off of, as generation references previous lines.
@@ -203,7 +177,8 @@ void World::generateFunc(unsigned int width,float(*func)(float)){
unsigned int i;
if((lineCount = width) <= 0)
abort();
- line=(struct line_t *)calloc(lineCount,sizeof(struct line_t));
+ line = new struct line_t[lineCount]; //(struct line_t *)calloc(lineCount,sizeof(struct line_t));
+ memset(line,0,lineCount*sizeof(struct line_t));
for(i=0;i<lineCount;i++){
line[i].y=func(i);
if(line[i].y<0)line[i].y=0;
@@ -222,19 +197,19 @@ void World::generateFunc(unsigned int width,float(*func)(float)){
}
World::~World(void){
- free(line);
+ delete[] line;
}
void World::update(Player *p,unsigned int delta){
p->loc.y+= p->vel.y *delta;
p->loc.x+=(p->vel.x*p->speed)*delta;
-
+
for(auto &e : entity){
if(e->type != STRUCTURET)
e->loc.x += e->vel.x * delta;
e->loc.y += e->vel.y * delta;
if(e->vel.x < 0)e->left = true;
- else if(e->vel.x > 0)e->left = false;
+ else if(e->vel.x > 0)e->left = false;
}
}
@@ -760,7 +735,7 @@ void World::addNPC(float x,float y){
entity.push_back(npc.back());
}
-void World::addObject(int i, bool q, char *p, float x, float y){
+void World::addObject(ITEM_ID i, bool q, const char *p, float x, float y){
object.push_back(new Object(i,q, p));
object.back()->spawn(x,y);
@@ -768,7 +743,7 @@ void World::addObject(int i, bool q, char *p, float x, float y){
}
/*void World::removeObject(Object i){
- object.delete(i);
+ object.delete[](i);
}*/
/*
@@ -861,7 +836,7 @@ IndoorWorld::IndoorWorld(void){
}
IndoorWorld::~IndoorWorld(void){
- free(line);
+ delete[] line; //free(line);
}
void IndoorWorld::generate(unsigned int width){ // Generates a flat area of width 'width'
@@ -869,7 +844,8 @@ void IndoorWorld::generate(unsigned int width){ // Generates a flat area of wid
lineCount=width+GEN_INC; // Sets line count to the desired width plus GEN_INC to remove incorrect line calculations.
if(lineCount<=0)abort();
- line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)); // Allocate memory for the array 'line'
+ line = new struct line_t[lineCount]; //(struct line_t *)calloc(lineCount,sizeof(struct line_t)); // Allocate memory for the array 'line'
+ memset(line,0,lineCount*sizeof(struct line_t));
for(i=0;i<lineCount;i++){ // Indoor areas don't have to be directly on the ground (i.e. 0)...
line[i].y=INDOOR_FLOOR_HEIGHT;