struct VirtualStream * createVirtualStream(char * filename) { //Allocate a virtual stream structure struct VirtualStream * newstream = (struct VirtualStream *) malloc(sizeof(struct VirtualStream)); if (newstream==0) { fprintf(stderr,"Cannot allocate memory for new stream\n"); return 0; } //Clear the whole damn thing.. memset(newstream,0,sizeof(struct VirtualStream)); if (filename!=0) { fprintf(stderr,"strncpy from %p to %p \n",filename,newstream->filename); //strncpy(newstream->filename,filename,MAX_PATH); myStrCpy(newstream->filename,filename,MAX_PATH); fprintf(stderr,"strncpy returned\n"); if (!readVirtualStream(newstream)) { fprintf(stderr,"Could not read Virtual Stream from file %s \n",filename); destroyVirtualStream(newstream); return 0; } } else { fprintf(stderr,"Created an empty virtual stream\n"); } return newstream; }
VAR * AddUnitVar(VTYPE Type, char * Name, UNIT * Owner) { VAR * pNewVar = malloc(sizeof(VAR)); pNewVar->Name = myStrCpy(Name); pNewVar->Type = Float; if (Type == Int) { int *x = malloc(sizeof(int)); *x = 0; pNewVar->Data = x; } else if (Type == Float) { float *x = malloc(sizeof(float)); *x = 0.0; pNewVar->Data = x; } else if (Type == Vector) { VECTOR *x = malloc(sizeof(VECTOR)); *x = NewVector(0, 0); pNewVar->Data = x; } else if (Type == String) { char **x = malloc(sizeof(char*)); *x = NULL; pNewVar->Data = x; } else if (Type == Color) { COLOR newCol = { 1, 1, 1, 1 }; COLOR *x = malloc(sizeof(COLOR)); *x = newCol; pNewVar->Data = x; } else if (Type == Bool) { BOOL *x = malloc(sizeof(BOOL)); *x = False; pNewVar->Data = x; } else if (Type == Char) { char *x = malloc(sizeof(char)); *x = '\0'; pNewVar->Data = x; } else if (Type == Matrix) { MATRIX *temp = (MATRIX*)malloc(sizeof(MATRIX)); SecureZeroMemory(temp->m, sizeof(float) * 9); pNewVar->Data = temp; } pNewVar->nextVar = Owner->nextVar; Owner->nextVar = pNewVar; return pNewVar; return NULL; }
String& String::operator=(const String& other) { if(this != &other) { if(data != NULL) delete[] data; myStrCpy(data, other.data); length = myStrLen(data); } return *this; }
//Adds a Unit of the given Archetype to the given level with the default Transform. UNIT * AddUnit(LEVEL *pLevel, ARCHETYPE *pArchetype, char *Name) { UNIT * pNewUnit = malloc(sizeof(UNIT)); pNewUnit->pInitTransform = malloc(sizeof(TRANSFORM)); pNewUnit->pTransform = malloc(sizeof(TRANSFORM)); pNewUnit->Name = myStrCpy(Name); pNewUnit->Tag = pArchetype->Tag; pNewUnit->pInitArchetype = pArchetype; *(pNewUnit->pInitTransform) = *(pArchetype->pGame->pGameStats->pDefaultTransform); *(pNewUnit->pTransform) = *(pArchetype->pGame->pGameStats->pDefaultTransform); pNewUnit->pLevel = pLevel; pNewUnit->nextVar = NULL; pNewUnit->nextUnit = pLevel->nextUnit; pLevel->nextUnit = pNewUnit; return pNewUnit; }
//Creates an empty Level. LEVEL * AddLevel(GAME * pGame, char *Name, int Order) { LEVEL * pNewLevel = malloc(sizeof(LEVEL)); CAMERA * pCam = malloc(sizeof(CAMERA)); pCam->Position = NewVector(0, 0); pCam->Zoom = 48; pNewLevel->Name = myStrCpy(Name); pNewLevel->Order = Order; pNewLevel->pCamera = pCam; pNewLevel->nextUnit = NULL; pNewLevel->pGame = pGame; pNewLevel->nextLevel = pGame->nextLevel; pGame->nextLevel = pNewLevel; return pNewLevel; }
void MultipleAnimations(char * Buffer, int NumberOfAnimations, SPRITE * pSprite) { int useless; char textureInput1[MAX_LENGTH]; char textureInput2[MAX_LENGTH]; char textureInput3[MAX_LENGTH]; char textureInput4[MAX_LENGTH]; char textureInput5[MAX_LENGTH]; switch (NumberOfAnimations) { case 1: sscanf(Buffer, "\tTextureFile = %i , %s", &useless, &textureInput1); pSprite->pImage->TextureFile = myStrCpy(textureInput1); break; case 2: sscanf(Buffer, "\tTextureFile = %i , %s , %s", &useless, &textureInput1, &textureInput2); pSprite->pImage->TextureFile = myStrCpy(textureInput1); pSprite->pImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->TextureFile = myStrCpy(textureInput2); pSprite->pImage->pNextImage->pNextImage = NULL; break; case 3: sscanf(Buffer, "\tTextureFile = %i , %s , %s , %s", &useless, &textureInput1, &textureInput2, &textureInput3); pSprite->pImage->TextureFile = myStrCpy(textureInput1); pSprite->pImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->TextureFile = myStrCpy(textureInput2); pSprite->pImage->pNextImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->pNextImage->TextureFile = myStrCpy(textureInput3); pSprite->pImage->pNextImage->pNextImage->pNextImage = NULL; break; case 4: sscanf(Buffer, "\tTextureFile = %i , %s , %s , %s , %s", &useless, &textureInput1, &textureInput2, &textureInput3, &textureInput4); pSprite->pImage->TextureFile = myStrCpy(textureInput1); pSprite->pImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->TextureFile = myStrCpy(textureInput2); pSprite->pImage->pNextImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->pNextImage->TextureFile = myStrCpy(textureInput3); pSprite->pImage->pNextImage->pNextImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->pNextImage->pNextImage->TextureFile = myStrCpy(textureInput4); pSprite->pImage->pNextImage->pNextImage->pNextImage->pNextImage = NULL; break; case 5: sscanf(Buffer, "\tTextureFile = %i , %s , %s , %s , %s , %s", &useless, &textureInput1, &textureInput2, &textureInput3, &textureInput4, &textureInput5); pSprite->pImage->TextureFile = myStrCpy(textureInput1); pSprite->pImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->TextureFile = myStrCpy(textureInput2); pSprite->pImage->pNextImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->pNextImage->TextureFile = myStrCpy(textureInput3); pSprite->pImage->pNextImage->pNextImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->pNextImage->pNextImage->TextureFile = myStrCpy(textureInput4); pSprite->pImage->pNextImage->pNextImage->pNextImage->pNextImage = malloc(sizeof(IMAGE)); pSprite->pImage->pNextImage->pNextImage->pNextImage->pNextImage->TextureFile = myStrCpy(textureInput5); pSprite->pImage->pNextImage->pNextImage->pNextImage->pNextImage->pNextImage = NULL; break; default: sscanf(Buffer, " %s", &textureInput1); pSprite->pImage->TextureFile = myStrCpy(textureInput1); } }
void InterpretArchetype(FILE * fpArch) { char buffer[MAX_LENGTH]; ARCHETYPE * pNewArchetype = malloc(sizeof(ARCHETYPE)); COMPONENT * pCurrComp = NULL; pNewArchetype->Name = "Untitled"; pNewArchetype->nextArchetype = NULL; pNewArchetype->nextComponent = NULL; pNewArchetype->pGame = pTheGame; pNewArchetype->pUnit = NULL; pNewArchetype->nextArchetype = pTheGame->nextArchetype; pTheGame->nextArchetype = pNewArchetype; while (!feof(fpArch)) { if (fgets(buffer, MAX_LENGTH, fpArch)) { char question[MAX_LENGTH]; int inputInt = 0; float inputFloat = 0.0f; VECTOR inputVector = NewVector(0, 0); AddNull(buffer); if (buffer[0] != '#' && strlen(buffer) > 2) { sscanf(buffer, "%s", &question); if (myStrCmp(question, "Name") <= 0) { char nameInput[MAX_LENGTH]; sscanf(buffer, "Name = %s", nameInput); pNewArchetype->Name = myStrCpy(nameInput); continue; } if (myStrCmp(question, "Tag") <= 0) { char tagInput[MAX_LENGTH]; sscanf(buffer, "Tag = %s", tagInput); pNewArchetype->Tag = GetTagFromString(tagInput); continue; } if (myStrCmp(question, "COMPONENT") <= 0) { COMPONENTTYPE theType; char typeInput[MAX_LENGTH]; sscanf(buffer, "COMPONENT |%s|", &typeInput); if (myStrCmp(typeInput, "Sprite") <= 0) theType = Sprite; if (myStrCmp(typeInput, "Mesh") <= 0) theType = Mesh; if (myStrCmp(typeInput, "Behavior") <= 0) theType = Behavior; if (myStrCmp(typeInput, "Physics") <= 0) theType = Physics; if (myStrCmp(typeInput, "Collider") <= 0) theType = Collider; if (myStrCmp(typeInput, "KSound") <= 0) theType = KSound; pCurrComp = AddComponent(pNewArchetype, theType); continue; } if (pCurrComp) { if (pCurrComp->Type == Mesh) { MESH * pMesh = (MESH*)pCurrComp->pStruct; if (myStrCmp(question, "Size") <= 0) { sscanf(buffer, "\tSize = (%f, %f)", &inputVector.x, &inputVector.y); pMesh->Size = inputVector; continue; } if (myStrCmp(question, "Color") <= 0) { COLOR inputColor; sscanf(buffer, "\tColor = (%f, %f, %f, %f)", &inputColor.r, &inputColor.g, &inputColor.b, &inputColor.a); pMesh->Color = inputColor; continue; } if (myStrCmp(question, "Opacity") <= 0) { sscanf(buffer, "\tOpacity = %f", &inputFloat); pMesh->Opacity = inputFloat; continue; } } if (pCurrComp->Type == Behavior) { BEHAVIOR * pBehavior = (BEHAVIOR*)pCurrComp->pStruct; if (myStrCmp(question, "BehaviorScript") <= 0) { char scriptInput[MAX_LENGTH]; sscanf(buffer, "\tBehaviorScript = %s", &scriptInput); pBehavior->BehaviorScript = GetBehaviorFromString(scriptInput); continue; } } if (pCurrComp->Type == Physics) { PHYSICS * pPhysics = (PHYSICS*)pCurrComp->pStruct; if (myStrCmp(question, "Gravity") <= 0) { sscanf(buffer, "\tGravity = %f", &inputFloat); pPhysics->Gravity = inputFloat; continue; } if (myStrCmp(question, "MaxSpeed") <= 0) { sscanf(buffer, "\tMaxSpeed = %f", &inputFloat); pPhysics->MaxSpeed = inputFloat; continue; } if (myStrCmp(question, "Friction") <= 0) { sscanf(buffer, "\tFriction = %f", &inputFloat); pPhysics->Friction = inputFloat; continue; } if (myStrCmp(question, "Velocity") <= 0) { sscanf(buffer, "\tVelocity = (%f, %f)", &inputVector.x, &inputVector.y); pPhysics->Velocity = inputVector; continue; } if (myStrCmp(question, "Acceleration") <= 0) { sscanf(buffer, "\tAcceleration = (%f, %f)", &inputVector.x, &inputVector.y); pPhysics->Acceleration = inputVector; continue; } } if (pCurrComp->Type == Collider) { COLLIDER * pCollider = (COLLIDER*)pCurrComp->pStruct; if (myStrCmp(question, "Offset") <= 0) { sscanf(buffer, "\tOffset = (%f, %f)", &inputVector.x, &inputVector.y); pCollider->Offset = inputVector; continue; } if (myStrCmp(question, "Height") <= 0) { sscanf(buffer, "\tHeight = %f", &inputFloat); pCollider->Height = inputFloat; continue; } if (myStrCmp(question, "Width") <= 0) { sscanf(buffer, "\tWidth = %f", &inputFloat); pCollider->Width = inputFloat; continue; } if (myStrCmp(question, "IsGhosted") <= 0) { sscanf(buffer, "\tIsGhosted = %i", &inputInt); pCollider->IsGhosted = inputInt; continue; } } if (pCurrComp->Type == KSound) { KSOUND * pSound = (KSOUND*)pCurrComp->pStruct; if (myStrCmp(question, "Volume") <= 0) { sscanf(buffer, "\tVolume = %f", &inputFloat); pSound->Volume = inputFloat; continue; } if (myStrCmp(question, "Positional") <= 0) { sscanf(buffer, "\tPositional = %i", &inputInt); pSound->Positional = inputInt; continue; } if (myStrCmp(question, "MaxReach") <= 0) { sscanf(buffer, "\tMaxReach = %f", &inputFloat); pSound->MaxReach = inputFloat; continue; } if (myStrCmp(question, "SoundFile") <= 0) { char scriptInput[MAX_LENGTH]; sscanf(buffer, "\tSoundFile = %s", &scriptInput); pSound->SoundFile = scriptInput; continue; } if (myStrCmp(question, "PlayOnStart") <= 0) { sscanf(buffer, "\tPlayOnStart = %i", &inputInt); pSound->PlayOnStart = inputInt; continue; } // TODO : Add the rest of the params. } if (pCurrComp->Type == Sprite) { SPRITE * pSprite = (SPRITE*)pCurrComp->pStruct; if (myStrCmp(question, "TextureFile") <= 0) { int temp; char textureInput[MAX_LENGTH]; sscanf(buffer, "\tTextureFile = %i , ", &temp); MultipleAnimations(buffer, temp, pSprite); continue; } if (myStrCmp(question, "Animated") <= 0) { sscanf(buffer, "\tAnimated = %i", &inputInt); pSprite->Animated = inputInt; continue; } if (myStrCmp(question, "RowCol") <= 0) { sscanf(buffer, "\tRowCol = (%f, %f)", &inputVector.x, &inputVector.y); pSprite->RowCol = inputVector; continue; } if (myStrCmp(question, "Offset") <= 0) { sscanf(buffer, "\tOffset = (%f, %f)", &inputVector.x, &inputVector.y); pSprite->Offset = inputVector; continue; } if (myStrCmp(question, "AnimationSpeed") <= 0) { sscanf(buffer, "\tAnimationSpeed = %f", &inputFloat); pSprite->AnimationSpeed = inputFloat; continue; } } if (myStrCmp(question, "EndComponent") <= 0) { pCurrComp = NULL; continue; } } } } } }
void InterpretLevel(FILE * fpLevel) { char buffer[MAX_LENGTH]; LEVEL * pNewLevel = malloc(sizeof(LEVEL)); CAMERA * pCam = malloc(sizeof(CAMERA)); UNIT * pCurrUnit = NULL; pCam->Position = NewVector(0, 0); pCam->Zoom = 48; pNewLevel->Name = "Untitled"; pNewLevel->Order = 0; pNewLevel->pCamera = pCam; pNewLevel->nextUnit = NULL; pNewLevel->pGame = pTheGame; pNewLevel->nextLevel = pTheGame->nextLevel; pTheGame->nextLevel = pNewLevel; while (!feof(fpLevel)) { if (fgets(buffer, MAX_LENGTH, fpLevel)) { char question[MAX_LENGTH]; int inputInt = 0; float inputFloat = 0.0f; VECTOR inputVector = NewVector(0, 0); AddNull(buffer); if (buffer[0] != '#' && strlen(buffer) > 2) { sscanf(buffer, "%s", &question); if ((myStrCmp(question, "Name") <= 0) && pCurrUnit == NULL) { char nameInput[MAX_LENGTH]; sscanf(buffer, "Name = %s", nameInput); pNewLevel->Name = myStrCpy(nameInput); continue; } if (myStrCmp(question, "Order") <= 0) { char nameInput[MAX_LENGTH]; sscanf(buffer, "Order = %i", &inputInt); pNewLevel->Order = inputInt; continue; } if (myStrCmp(question, "UNIT") <= 0) { char archInput[MAX_LENGTH]; char nameInput[MAX_LENGTH]; ARCHETYPE * pArchetype = NULL; sscanf(buffer, "UNIT < %s > %s", &archInput, &nameInput); pArchetype = FindArchetypeByName(pTheGame, archInput); pCurrUnit = AddUnit(pNewLevel, pArchetype, myStrCpy(nameInput)); continue; } if (pCurrUnit) { if (myStrCmp(question, "Tag") <= 0) { char tagInput[MAX_LENGTH]; sscanf(buffer, "\tTag = %s", tagInput); pCurrUnit->Tag = GetTagFromString(tagInput); continue; } if (myStrCmp(question, "InitialPosition") <= 0) { sscanf(buffer, "\tInitialPosition = (%f, %f)", &inputVector.x, &inputVector.y); pCurrUnit->pInitTransform->Position = inputVector; continue; } if (myStrCmp(question, "InitialRotation") <= 0) { sscanf(buffer, "\tInitialRotation = %f", &inputFloat); pCurrUnit->pInitTransform->Rotation = inputFloat; continue; } if (myStrCmp(question, "InitialScale") <= 0) { sscanf(buffer, "\tInitialScale = (%f, %f)", &inputVector.x, &inputVector.y); pCurrUnit->pInitTransform->Scale = inputVector; continue; } if (myStrCmp(question, "VAR") <= 0) { VTYPE theType; VAR * newVar; char varInput[MAX_LENGTH]; char typeInput[MAX_LENGTH]; char dataInput[MAX_LENGTH]; void * data; sscanf(buffer, "\tVAR %s : %s = %s", &varInput, &typeInput, &dataInput); theType = GetVTypeFromString(typeInput); newVar = AddUnitVar(theType, varInput, pCurrUnit); if (theType == Float) { float x; sscanf(dataInput, "%f", &x); data = malloc(sizeof(x)); *((float*)newVar->Data) = x; } else if (theType == Int) { int x; sscanf(dataInput, "%i", &x); data = malloc(sizeof(x)); *(int*)newVar->Data = x; } else if (theType == Vector) { VECTOR x; sscanf(dataInput, "(%f, %f)", &x.x, &x.y); data = malloc(sizeof(x)); *(VECTOR*)newVar->Data = x; } else if (theType == String) { char * x; x = myStrCpy(dataInput); data = malloc(sizeof(x)); *(char**)newVar->Data = x; } else if (theType == Bool) { BOOL x; sscanf(dataInput, "%i", &x); data = malloc(sizeof(x)); *(BOOL*)newVar->Data = x; } else if (theType == Color) { COLOR x; sscanf(dataInput, "(%f, %f, %f, %f)", &x.r, &x.g, &x.b, &x.a); data = malloc(sizeof(x)); *(COLOR*)newVar->Data = x; } else if (theType == Char) { char x; sscanf(dataInput, "%c", &x); data = malloc(sizeof(x)); *(char*)newVar->Data = x; } else if (theType == Matrix) { MATRIX x; sscanf(dataInput, "{ {%f, %f, %f} {%f, %f, %f} {%f, %f, %f} }", &x.m[0][0], &x.m[0][1], &x.m[0][2], &x.m[1][0], &x.m[1][1], &x.m[1][2], &x.m[2][0], &x.m[2][1], &x.m[2][2]); data = malloc(sizeof(x)); *(MATRIX*)newVar->Data = x; } } } if (myStrCmp(question, "EndUnit") <= 0) { pCurrUnit = NULL; continue; } } } } }
String::String(const String& other) { myStrCpy(data, other.data); length = other.length; }
String::String(char* data) { myStrCpy(this->data, data); length = myStrLen(this->data); }
ARCHETYPE * CreateInstanceOfArchetype(ARCHETYPE * pArchetype, UNIT * pUnit) { ARCHETYPE * pNewArchetype = malloc(sizeof(ARCHETYPE)); COMPONENT * temp = pArchetype->nextComponent; //pNewArchetype->Name = strcat(pArchetype->Name, "(Instance)"); pNewArchetype->Tag = pUnit->Tag; pNewArchetype->Name = myStrCpy(pArchetype->Name); pNewArchetype->nextComponent = NULL; pNewArchetype->nextArchetype = NULL; pNewArchetype->pGame = pArchetype->pGame; pNewArchetype->pUnit = pUnit; while (temp) { COMPONENT * compCopy = malloc(sizeof(COMPONENT)); compCopy->pArchetype = pNewArchetype; compCopy->Type = temp->Type; if (temp->Type == Sprite) { SPRITE * spriteCopy = malloc(sizeof(SPRITE)); *spriteCopy = *((SPRITE *) temp->pStruct); spriteCopy->pArchetype = pNewArchetype; spriteCopy->pComponent = compCopy; compCopy->pStruct = spriteCopy; } else if (temp->Type == Mesh) { MESH * sqmeshCopy = malloc(sizeof(MESH)); *sqmeshCopy = *((MESH *)temp->pStruct); sqmeshCopy->pArchetype = pNewArchetype; sqmeshCopy->pComponent = compCopy; compCopy->pStruct = sqmeshCopy; } else if (temp->Type == Behavior) { BEHAVIOR * behCopy = malloc(sizeof(BEHAVIOR)); *behCopy = *((BEHAVIOR *)temp->pStruct); behCopy->pArchetype = pNewArchetype; behCopy->pComponent = compCopy; compCopy->pStruct = behCopy; } else if (temp->Type == Physics) { PHYSICS * physCopy = malloc(sizeof(PHYSICS)); *physCopy = *((PHYSICS *)temp->pStruct); physCopy->pArchetype = pNewArchetype; physCopy->pComponent = compCopy; compCopy->pStruct = physCopy; } else if (temp->Type == Collider) { COLLIDER * collCopy = malloc(sizeof(COLLIDER)); *collCopy = *((COLLIDER *)temp->pStruct); collCopy->pArchetype = pNewArchetype; collCopy->pComponent = compCopy; compCopy->pStruct = collCopy; } else if (temp->Type == KSound) { KSOUND * soundCopy = malloc(sizeof(KSOUND)); *soundCopy = *((KSOUND *)temp->pStruct); soundCopy->pArchetype = pNewArchetype; soundCopy->pComponent = compCopy; compCopy->pStruct = soundCopy; } compCopy->nextComponent = pNewArchetype->nextComponent; pNewArchetype->nextComponent = compCopy; temp = temp->nextComponent; } return pNewArchetype; }
void * AddVar(VTYPE Type, char * Name, BEHAVIOR * Owner) { VAR * pNewVar = malloc(sizeof(VAR)); pNewVar->Name = myStrCpy(Name); pNewVar->Type = Float; pNewVar->nextVar = NULL; // NOTE: SHOULD MAKE A SWITCH CASE. MUCH CLEANER (AND SOMETIMES FASTER!) if (Type == Int) { int *x = malloc(sizeof(int)); *x = 0; pNewVar->Data = x; } else if (Type == Float) { float *x = malloc(sizeof(float)); *x = 0.0; pNewVar->Data = x; } else if (Type == Vector) { VECTOR *x = malloc(sizeof(VECTOR)); *x = NewVector(0,0); pNewVar->Data = x; } else if (Type == String) { char **x = malloc(sizeof(char*)); *x = NULL; pNewVar->Data = x; } else if (Type == Color) { COLOR newCol = { 1, 1, 1, 1 }; COLOR *x = malloc(sizeof(COLOR)); *x = newCol; pNewVar->Data = x; } else if (Type == Bool) { BOOL *x = malloc(sizeof(BOOL)); *x = False; pNewVar->Data = x; } else if (Type == Char) { char *x = malloc(sizeof(char)); *x = '\0'; pNewVar->Data = x; } else if (Type == Matrix) { MATRIX *temp = (MATRIX*)malloc(sizeof(MATRIX)); SecureZeroMemory(temp->m, sizeof(float)* 9); pNewVar->Data = temp; } pNewVar->nextVar = Owner->nextVar; Owner->nextVar = pNewVar; return pNewVar->Data; }