Dumb-ways-to-memorize
2D game
Functions
mymath.c File Reference
#include "globals.h"
#include <string.h>
#include <stdlib.h>
+ Include dependency graph for mymath.c:

Go to the source code of this file.

Functions

int LargestDivisor (int num)
 
void Vec2Add (vec2_t *A, vec2_t *B, vec2_t *C)
 
void Vec2Subtract (vec2_t *First, vec2_t *Second, vec2_t *C)
 
void Vec2MultiplyScalar (vec2_t *A, int B, vec2_t *C)
 
int CountMem (void *src, int size_type)
 
int AllocateDynamic (void **dst, void *src, int size_type, int size)
 
int CompareMemToMemArray (void *mem, void *mem_array, int size_type, int size_array)
 

Function Documentation

int AllocateDynamic ( void **  dst,
void *  src,
int  size_type,
int  size 
)

Allocate memory and copy over src into it. Adds Null to end. Returns NULL on size 0

Parameters
[in,out]dstIf non-null, destination for the allocation.
[in,out]srcIf non-null, source for the adding.
size_typeSize of the type.
sizeThe size.
Returns
0 on success, -1 on error.
Author
Anthony Rios
Date
2/1/2016

Definition at line 66 of file mymath.c.

Referenced by CopyObjectToObjectArray(), and ParseToObject().

67 {
68  int offset = size_type/sizeof(int);
69  if(!dst) return -1;
70  *dst = realloc(*dst, size_type*(size+1));
71  if(!*dst) return -1;
72  src ? memcpy((int*)(*dst)+(size-1)*offset, src, size_type) : memset((int*)(*dst)+(size-1)*offset, 0, size_type);
73  memset((int*)(*dst)+(size)*offset, 0, size_type);
74  return 0;
75 }
int CompareMemToMemArray ( void *  mem,
void *  mem_array,
int  size_type,
int  size_array 
)

Compare memory to memory array.

Parameters
[in,out]memIf non-null, the memory.
[in,out]mem_arrayIf non-null, array of memories.
size_typeSize of type, via sizeof() function.
size_arraySize of Array.
Returns
0 if equal, -1 if not.
Author
Anthony Rios
Date
1/31/2016

Definition at line 77 of file mymath.c.

Referenced by RandomizeSelectedLevels(), and SelectLevels().

78 {
79  int i, offset;
80  int *memory = (int*) mem_array;
81  offset = size_type;
82  for(i = 0; i < size_array; i++)
83  {
84  if(!memcmp(mem, memory, size_type))
85  {
86  return 0;
87  }
88  memory += offset;
89  }
90  return -1;
91 }
int CountMem ( void *  src,
int  size_type 
)

Counts the memory of type size_type, given that the final address is null.

Parameters
[in,out]srcIf non-null, source of memory.
size_typeSize of the type.
Returns
The total number of memory, if src is null 0 is returned.
Author
Anthony Rios
Date
2/1/2016

Definition at line 51 of file mymath.c.

Referenced by CopyObjectToObjectArray(), CountObjectChildren(), CountObjectMembers(), DoPlayerThink(), FindKey(), FindMenuItem(), FindPower(), FindValue(), IncrementFrame(), LoadEntityData(), LoadLevel(), LoadLevelData(), LoadMenu(), LoadMenuData(), LoadPowerUpData(), ParseAI(), ParsePresetAI(), ParseToEntity(), ParseToStringArray(), PrintObject(), ProcessMenuItemsByType(), RandomizeSelectedLevels(), StrToHazard(), TouchGeneric(), TouchGoal(), TouchPlayer(), UpdatePowerSelectMenu(), UpdatePowerUpMenu(), and UpdateVerticalMenu().

52 {
53  int i, offset;
54  int *source = (int*)src;
55  if(source == NULL)
56  return 0;
57  i = 0; offset = size_type/sizeof(int);
58  while( *(source) )
59  {
60  source += offset;
61  i++;
62  }
63  return i;
64 }
int LargestDivisor ( int  num)

Definition at line 5 of file mymath.c.

Referenced by ProcessMenuItemsByType().

6 {
7  int i, root, hit;
8  hit = 0;
9  root = num>>1;
10  for(i = 0; i < root; i++)
11  {
12  if(!num%i)
13  {
14  hit = i;
15  }
16  }
17  return hit;
18 }
void Vec2Add ( vec2_t A,
vec2_t B,
vec2_t C 
)

Definition at line 20 of file mymath.c.

References vec2_t::x, and vec2_t::y.

Referenced by DoCollision(), JumpAI(), MoveAI(), RunPhysics(), Spawn(), and WalkAI().

21 {
22  if(!A || !B || !C)
23  {
24  return;
25  }
26  C->x = A->x + B->x;
27  C->y = A->y + B->y;
28 }
int y
Definition: globals.h:22
int x
Definition: globals.h:21
void Vec2MultiplyScalar ( vec2_t A,
int  B,
vec2_t C 
)

Definition at line 41 of file mymath.c.

References vec2_t::x, and vec2_t::y.

Referenced by JumpAI(), MoveAI(), and WalkAI().

42 {
43  if(!A || !C)
44  {
45  return;
46  }
47  (*C).x = A->x * B;
48  (*C).y = A->y * B;
49 }
int y
Definition: globals.h:22
int x
Definition: globals.h:21
void Vec2Subtract ( vec2_t First,
vec2_t Second,
vec2_t C 
)

Definition at line 31 of file mymath.c.

References vec2_t::x, and vec2_t::y.

32 {
33  if(!First || !Second || !C)
34  {
35  return;
36  }
37  C->x = First->x - Second->x;
38  C->y = First->y - Second->y;
39 }
int y
Definition: globals.h:22
int x
Definition: globals.h:21