-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux.cpp
276 lines (237 loc) · 8.51 KB
/
linux.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <SDL2/SDL.h>
#include <pthread.h>
#include "include/linux.hpp"
#include <unistd.h>
#include "include/keyboard.hpp"
#include "include/types.hpp"
#include <assert.h>
#include <png.h>
#include <iostream>
extern const unsigned int screenwidth,screenheight;
pthread_t thread;
unsigned int* pix;
static SDL_Texture* fbtex;
static SDL_Renderer* renderer;
SDL_Event event;
volatile char syncvar;
void platspec_sync(){
syncvar=0;
while(!syncvar);
}
#define tmp (unsigned (1 - tx) * (1 - ty)
void platspec_creategamethread(void*(*func)(void*)){
pthread_create(&thread,NULL,func,NULL);
}
void platspec_sleep(double dur){
usleep(dur);
}
void* loop(void* unused){
unused=0;
while(1){
syncvar=1;
while(syncvar==1);
SDL_UpdateTexture(fbtex,NULL,pix,screenwidth*4);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,fbtex,NULL,NULL);
SDL_RenderPresent(renderer);
if(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
keybuff_write(event.key.keysym.sym);
break;
}
}
SDL_PumpEvents();
extern unsigned char* keyarray;
keyarray=(unsigned char*)SDL_GetKeyboardState(NULL);
}
}
void* platspec_getframebuffer(){
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* win=SDL_CreateWindow("BalashEngine",SDL_WINDOWPOS_UNDEFINED|SDL_WINDOW_OPENGL,SDL_WINDOWPOS_UNDEFINED,screenwidth,screenheight,0);
renderer=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
fbtex=SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,screenwidth,screenheight);
pix=(unsigned int*)malloc(sizeof(unsigned int [screenwidth*screenheight])+100);
pthread_create(&thread,NULL,loop,NULL);
return pix;
}
#ifndef _UTIL_CPP //disable it for now so it'll compile
//don't forget to free, it is malloced
texturewh platspec_loadTexture(const char *filename, unsigned int widthin, unsigned int heightin)
{
unsigned int width,height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
png_bytep*row_pointers;
char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen(filename, "rb");
if (!fp)
perror("[read_png_file] File %s could not be opened for reading");
fread(header, 1, 8, fp);
if (png_sig_cmp((png_const_bytep)header, 0, 8))
perror("[read_png_file] File %s is not recognized as a PNG file");
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
perror("[read_png_file] png_create_read_struct failed");
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
perror("[read_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(png_ptr)))
perror("[read_png_file] Error during init_io");
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
if(heightin==0 || widthin==0){
heightin=height;
widthin=width;
}
/* Read any color_type into 8bit depth, RGBA format. */
/* See http://www.libpng.org/pub/png/libpng-manual.txt */
if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
/* PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth. */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
/* These color_type don't have an alpha channel then fill it with 0xff. */
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE)
png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
png_read_update_info(png_ptr, info_ptr);
row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
for (unsigned int y = 0; y < height; y++)
{
row_pointers[y] = (png_bytep )malloc(png_get_rowbytes(png_ptr, info_ptr));
}
png_read_image(png_ptr, (png_bytep*)row_pointers);
fclose(fp);
if(heightin==0 || widthin==0){
heightin=height;
widthin=width;
}
size_t imgbytes = widthin * heightin +8;
texturewh texture;
texture.raw = new unsigned int[imgbytes];
texture.width=widthin;
texture.height=heightin;
for (int y = 0; y <(int) heightin; y++)
{
for (unsigned int x = 0; x < widthin; x++)
{
unsigned int * ptr = (unsigned int *)(&row_pointers[y%height][x%width * 4]);
texture.raw[x+y*widthin]=__builtin_bswap32(*ptr);
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
for (unsigned int y = 0; y < heightin; y++)
{
free(row_pointers[y]);
}
free(row_pointers);
png_free(png_ptr,NULL);
return texture;
}
//don't forget to delete, it is malloced
object platspec_loadOBJ(const char * filename){// ( -_・) ︻デ═一 ▸
FILE * obj = fopen(filename,"r");
char * buffer;//buffer to read lines of file
char * fstr;//Face STRing
char * vstr;//Value STRing
size_t len = 0;//file shit
ssize_t read;//file shit
size_t m=0;//loop counter
size_t v_count=0;
size_t uv_count=0;
size_t f_count=0;
while ((read = getline(&buffer, &len, obj)) != -1){
if(buffer[0]=='v' && buffer[1]==' '){
v_count++;
}
if(buffer[0]=='v' && buffer[1]=='t'){
uv_count++;
}
if(buffer[0]=='f' && buffer[1]==' '){
f_count++;
}
}//get amount of vertices and faces
rewind (obj);
object ret = object(v_count,uv_count,f_count);//( -_・) ︻デ═一 ▸
ret.vertices = new vec3[v_count];
ret.faces = new face[f_count];
ret.f_count = f_count;
ret.v_count = v_count;
ret.uv_count = uv_count;
v_count=0;//
uv_count=0;
f_count=0;
while ((read = getline(&buffer, &len, obj)) != -1) {
if(buffer[0]=='v' && buffer[1]==' '){
char* shit [3];
strsep(&buffer, " ");
shit[0]=strsep(&buffer, " ");
shit[1]=strsep(&buffer, " ");
shit[2]=strsep(&buffer, " ");
ret.vertices[v_count]=vec3(
strtod(shit[0],0x0),
strtod(shit[1],0x0),
strtod(shit[2],0x0));
v_count++;
continue;
}
if(buffer[0]=='v' && buffer[1]=='t'){
char* shit [2];
strsep(&buffer, " ");
shit[0]=strsep(&buffer, " ");
shit[1]=strsep(&buffer, " ");
ret.uvertices[uv_count]=vec2(//make uv
strtod(shit[0],0x0),
strtod(shit[1],0x0));
uv_count++;
continue;
}
if(buffer[0]=='f' && buffer[1]==' '){
//f 2/1/1 3/2/1 4/3/1
strsep(&buffer, " ");//remove f letter
int shit [3];
int shit2 [3];
while((fstr = strsep(&buffer, " ")) != 0 ){//iter faces thru spaces. got 2/1/1 in fstr
vstr = strsep(&fstr, "/");//got 2 from fstr
shit[m]=atoi(vstr);
vstr = strsep(&fstr, "/");//got 1 from fstr
shit2[m]=atoi(vstr);
m++;
}
ret.faces[f_count]=face(3);
ret.faces[f_count].vertices[0]=ret.vertices[shit[0]-1];//TODO:
ret.faces[f_count].vertices[1]=ret.vertices[shit[1]-1];//do normal >3 face
ret.faces[f_count].vertices[2]=ret.vertices[shit[2]-1];//normal support
ret.faces[f_count].uvertices[0]=ret.uvertices[shit2[0]-1];//TODO:
ret.faces[f_count].uvertices[1]=ret.uvertices[shit2[1]-1];//do normal >3 face
ret.faces[f_count].uvertices[2]=ret.uvertices[shit2[2]-1];//normal support
//so now make it work. hard lessons
m=0;
f_count++;
}//parser works.
}
fclose(obj);
return ret;
}
#endif