-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
142 lines (117 loc) · 3.51 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "util.h"
#include "scene.h"
SDL_Surface *loadSurface = NULL;
SDL_Renderer *utilRenderer = NULL;
float toAngle(float num){
return num* 180 / 3.14159265;
}
float toRadian(float num){
return num * 3.14159265 / 180;
}
void initUtil(SDL_Renderer **Renderer){
utilRenderer = *Renderer;
}
int createTex(SDL_Texture **tex, const char *URL){
int pass = 1;
loadSurface = IMG_Load(URL);
if(loadSurface == NULL){
pass--;
}
*tex = SDL_CreateTextureFromSurface(utilRenderer, loadSurface);
SDL_SetTextureBlendMode(*tex, SDL_BLENDMODE_BLEND);
SDL_FreeSurface(loadSurface);
return pass;
}
SDL_Renderer *giveRenderer(){
return utilRenderer;
}
SDL_Rect findSheet(unsigned long width, unsigned long height, unsigned long rows, unsigned long columns, unsigned long number){//image width, image height
SDL_Rect tempRect;//used to select it
tempRect.x = 0;
tempRect.y = 0;
tempRect.w = 0;
tempRect.h = 0;
unsigned long tilewidth = width / columns;
unsigned long tileheight = height / rows;
unsigned long currectColumn = 0;
unsigned long currentRow = 0;
if(columns <= number){
while (columns <= number){
currentRow++;
number -= columns;
currectColumn = number;
}
} else {
currectColumn = number;
}
tempRect.x = currectColumn * tilewidth;
tempRect.y = currentRow * tileheight;
tempRect.w = tilewidth;
tempRect.h = tileheight;
return tempRect;
}
unsigned char *readFile(const char *location, unsigned long *sizePtr){
FILE *f;
unsigned long size;
unsigned char *dataPtr;
f = fopen(location, "rb");
if(f == NULL){
printf("Error cant find\n");
return (unsigned char *)NULL;
}
fseek(f,0,SEEK_END);
size = ftell(f);
rewind(f);
dataPtr = (unsigned char *)malloc(size + 1);
fread(dataPtr, sizeof(unsigned char), size, f);
if(dataPtr == NULL){
printf("Outta memory\n");
return (unsigned char *)NULL;
}
//printf("neat [%c]\n", dataPtr[1]);
fclose(f);
printf("Found %s which is %lu and the first byte is %c\n", location, size, dataPtr[0]);
*sizePtr = size;
return dataPtr;
}
unsigned long parseNumber(const char *search, unsigned char *data, unsigned char **endData){
unsigned long number = 0;
char *tempDataPtr = strstr((signed char *)data, search);//this sets this one up just fine
char *tempString = NULL;
unsigned long tempStringLen = 0;
if(tempDataPtr == NULL){
//if(global.isDebug){
printf("Couldnt find %s in TMX file\n", search);
//}
} else{
//printf("Found [%s]\n", search);
tempDataPtr += strlen(search);//gets to the end of found string in pplace
//printf("%c\n", *tempDataPtr);
if(isdigit(*tempDataPtr) != 0){//use levelWidth
while(isdigit(tempDataPtr[tempStringLen]) != 0){
tempStringLen++;
//printf("%c", tempDataPtr[tempStringLen]);
//printf("Num length %lu\n", tempStringLen);
}
tempString = (char *)calloc(tempStringLen + 1, sizeof(char));
memcpy(tempString, tempDataPtr, tempStringLen);
tempString[tempStringLen + 1] = '\0';
//printf("%s %lu\n", tempString, tempStringLen);
//number = (unsigned long)strtol(tempString, NULL, 10);//no
sscanf(tempString, "%lu", &number);
//number = 3;
//printf("Final: %lu\n", number);
if(endData != NULL)
*endData = (unsigned char *)(tempDataPtr + tempStringLen);
free(tempString);
}
//printf("\n");
}
return number;
}