summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 31bca71746c878da5b6c7b4a48594aed90c8cd5e (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <sstream>
#include <cmath>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include <SDL.h>
#include <SDL_audio.h>

#include "World.h"
#include "PerlinNoise.h"

#include <random>
using SDLInitType=std::pair<int,std::pair<SDL_Window*,SDL_Renderer*>>;

struct Context
{
	// initial window size
	int win_width=640;
	int win_height=480;

	// some other vars
	bool quit = false;

	Uint32 frameTime;
	Uint32 lastFrameTime = 0;
	Uint32 deltaTime = 0;
	Uint32 fpsTime = 0;
	Uint32 fpsCount = 0;
	Uint32 fps = 0;

	Uint32 mouse_x=0;
	Uint32 mouse_y=0;

	SDL_Renderer *ren;
	SDLInitType sdl_init;
	std::vector<SDL_Texture*> textures;

	int show_tiles_back=6;
	int show_tiles_front=10;
	int show_tiles_size=20;
	int show_tiles_vertical_move=12;
	int show_tiles_max_height=800;
	int show_tiles_water_level=-1;

	// init world LEVEL 1
	World world=1;

};


static Uint32 wavLength;
static Uint32 wavLength2;
static Uint8 *wavBuffer;
static Uint8 *wavBuffer2;

int sample=0;
int sample2=0;

void audiomixer(void *userdata, Uint8 *stream, int len)
{
	SDL_memset(stream, 0, len);  // make sure this is silence.
	
	int volume=15000;

	for(int i=0;i<len;i++)
	{
		if(sample<wavLength)
		{
			stream[i]+=wavBuffer[sample];
		}

		if(sample2>=wavLength2)
		{
			sample2=0;
		}

		stream[i]+=wavBuffer2[sample2];

		sample++;
		sample2++;
	}
	return;

    int samples = len / 4;
    short *buf = (short*)stream;

/*
    for (i = 0; i < samples*2; i+=2)
    {
        buf[i]=buf[i+1] = volume*sin(sample*440/4410/2);
	if((sample/44100)%2==1)buf[i]=buf[i+1]=0;
	buf[i]+=volume*sin(sample*261/4410/2);
	sample++;
    }
*/
		

}

void show_AudioSpec(SDL_AudioSpec *as)
{
	std::cout << "freq: " << as->freq << std::endl;

	std::cout << "bitsize: " << SDL_AUDIO_BITSIZE(as->format) << std::endl;
	std::cout << "float: " << (bool)SDL_AUDIO_ISFLOAT(as->format) << std::endl;
	std::cout << "bigendian: " << (bool)SDL_AUDIO_ISBIGENDIAN(as->format) << std::endl;
	std::cout << "signed: " << (bool)SDL_AUDIO_ISSIGNED(as->format) << std::endl;
	std::cout << "int: " << (bool)SDL_AUDIO_ISINT(as->format) << std::endl;
	std::cout << "littleendian: " << (bool)SDL_AUDIO_ISLITTLEENDIAN(as->format) << std::endl;
	std::cout << "unsigned: " << (bool)SDL_AUDIO_ISUNSIGNED(as->format) << std::endl;


	std::cout << "channels: " << (int)as->channels << std::endl;
	std::cout << "samples (buffer): " << as->samples << std::endl;
	std::cout << "callback func: " << as->callback << std::endl;
}

// audio from http://www.brainybetty.com/soundsforpowerpoint2.htm
// https://freesound.org/people/ProjectsU012/sounds/341695/
void sdl_play_sound()
{
	
	static bool first=true;
	static SDL_AudioSpec wavSpec;
	static SDL_AudioDeviceID deviceId;
	 
	if (first)
	{

		SDL_LoadWAV("assets/music.wav", &wavSpec, &wavBuffer2, &wavLength2);
		std::cout << "---music.wav---" << std::endl;
		show_AudioSpec(&wavSpec);
		SDL_LoadWAV("assets/coin.wav", &wavSpec, &wavBuffer, &wavLength);
		std::cout << "---coin.wav---" << std::endl;
		show_AudioSpec(&wavSpec);
		sample=wavLength;

		//std::cout << "---desired load wav---" << std::endl;
		//show_AudioSpec(&wavSpec);
/*
		if(spec==NULL)
		{	
		
			std::cout << "SDL_LoadWavError: " << SDL_GetError() << std::endl;
			exit(-1);
		}
		show_AudioSpec(spec);
*/
		// open most reasonable default device for playback. 
		wavSpec.callback = audiomixer;
		deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);
		std::cout << "--- target---" << std::endl;
		show_AudioSpec(&wavSpec);

		SDL_PauseAudioDevice(deviceId, 0);
		first=false;
	}

//	SDL_LoadWAV("died.wav", &wavSpec, &wavBuffer, &wavLength);
//	SDL_LoadWAV("music.wav", &wavSpec, &wavBuffer, &wavLength);
//	SDL_LoadWAV("win.wav", &wavSpec, &wavBuffer, &wavLength);

//	int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
#ifndef __EMSCRIPTEN__
#endif

	//SDL_CloseAudioDevice(deviceId);
	//SDL_FreeWAV(wavBuffer);
}

SDLInitType sdl_start(int win_width, int win_height)
{
	//First we need to start up SDL, and make sure it went ok
	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0){
		std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
		return {1,{nullptr,nullptr}};
	}
	std::cout << "SDL_Init OK" << std::endl;

	atexit(SDL_Quit);
	
	//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
	SDL_Window *win = SDL_CreateWindow("Hello World!", 200, 100, win_width, win_height, SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
	//Make sure creating our window went ok
	if (win == nullptr){
		std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
		return {1,{nullptr,nullptr}};
	}
	std::cout << "SDL_CreateWindow OK" << std::endl;

	//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
	//video driver supports the flags we're passing
	//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
	//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
	//synchronized with the monitor's refresh rate
	SDL_Renderer *ren = SDL_CreateRenderer(win, -1,0);//, SDL_RENDERER_SOFTWARE);// | SDL_RENDERER_PRESENTVSYNC|SDL_HINT_RENDER_VSYNC);
	if (ren == nullptr){
		SDL_DestroyWindow(win);
		std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
		return {1,{win,nullptr}};
	}
	std::cout << "SDL_CreateRenderer OK" << std::endl;

	return {0,{win,ren}};
}

void sdl_stop(SDLInitType sdl_init)
{
	if(sdl_init.second.second!=nullptr)SDL_DestroyRenderer(sdl_init.second.second);
	if(sdl_init.second.first!=nullptr)SDL_DestroyWindow(sdl_init.second.first);
}

SDL_Texture* sdl_load_texture(std::string imagePath,int key_r, int key_g, int key_b,SDL_Renderer *ren)
{
	//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
	//this lets us choose when to upload or remove textures from the GPU
	SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
	SDL_SetColorKey(bmp, SDL_TRUE, SDL_MapRGB(bmp->format, key_r, key_g, key_b));

	if (bmp == nullptr){
		std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
		return nullptr;
	}
	std::cout << "SDL_LoadBMP ("<<imagePath<<") OK"<<std::endl;

	SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);

	//We no longer need the surface
	SDL_FreeSurface(bmp);

	if (tex == nullptr){
		std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
		return nullptr;
	}
	std::cout << "SDL_CreateTextureFromSurface ("<<imagePath<<") OK"<<std::endl;
//       SDL_LockSurface(bmp4);
//       SDL_memset(bmp4->pixels,SDL_MapRGBA(bmp4->format, 255, 0, 255,220), bmp4->h * bmp4->pitch);
//       SDL_UnlockSurface(bmp4);

	//To use a hardware accelerated texture for rendering we can create one from
	//the surface we loaded

	return tex;
}

void sdl_put_letter(SDL_Renderer *ren, SDL_Texture *tex,int x,int y, int w, int h, int r, int g, int b, int ch)
{
	
		if(ch>=123&&ch<=126)ch-=123-40;
		else if(ch>=97&&ch<=122)ch-=97;
		else if(ch>=65&&ch<=96)ch-=65;
		else if(ch>=32&&ch<=63)ch-=32-50;
		else ch=81;

		int col=ch%10;
		int row=ch/10;
		SDL_SetTextureColorMod(tex,r,g,b);
		SDL_Rect src={col*8,row*10,8,10};
		SDL_Rect dst={x,y,w,h};
		SDL_RenderCopy(ren,tex,&src,&dst);

}

void sdl_put_str(SDL_Renderer *ren, SDL_Texture *tex,int x, int y,int size,std::string str,int r,int g, int b,int length,int win_width,int win_height,int which,int of)
{
		length++;
		int max_char_width=win_width/length;
		int max_char_height=max_char_width*10/8;
		int char_width=max_char_width*size/10;
		int char_height=max_char_height*size/10;

		int pos=0;
		x=(win_width-length*char_width)/2;

		if(y==-1)
		{
			y=win_height/2+(which-1)*max_char_height;
		}
		

		SDL_SetRenderDrawColor(ren, 40,40,40,  255);
		SDL_Rect rect2={0,y-2,win_width,char_height+4};
		SDL_RenderFillRect(ren,&rect2);

		for(char ch:str)
		{
			pos++;
			sdl_put_letter(ren,tex,x+char_width*pos,y, char_width,char_height, r,g,b, ch);
		}
}

//

void main_loop(void *arg)
{
		Context *ctx=(Context *)arg;
		SDL_Event event;

		// calc delta and FPS
		ctx->frameTime = SDL_GetTicks();
		ctx->deltaTime = ctx->frameTime - ctx->lastFrameTime;
		ctx->lastFrameTime = ctx->frameTime;
		ctx->fpsTime+=ctx->deltaTime;
		ctx->fpsCount++;

		if(ctx->fpsTime>1000)
		{
			ctx->fps=ctx->fpsCount*1000/ctx->fpsTime;
			ctx->fpsTime=0;
			ctx->fpsCount=0;
		}


		// handle events (mouse & kb & win resize)
		while(!ctx->quit && SDL_PollEvent(&event)) 
		{
			if (event.type == SDL_WINDOWEVENT && (event.window.event==SDL_WINDOWEVENT_RESIZED || event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED)) 
			{
//			   SDL_Log("Window %d size changed to %dx%d",
			   // event.window.windowID, event.window.data1,
			    //event.window.data2);
				ctx->win_width=event.window.data1;
				ctx->win_height=event.window.data2;

				ctx->show_tiles_size=ctx->win_height/15;
				ctx->show_tiles_back=ctx->win_width/ctx->show_tiles_size*0.25;
				ctx->show_tiles_front=ctx->win_width/ctx->show_tiles_size*1;
//				while(show_tiles_size%20!=0)show_tiles_size--;
			}

			if (event.type==SDL_MOUSEBUTTONDOWN)
			{
				ctx->mouse_x=event.button.x;
				ctx->mouse_y=event.button.y;

				ctx->world.mouseclick(((ctx->mouse_x+ctx->show_tiles_size/2-ctx->world.player.x2)/ctx->show_tiles_size+ctx->world.player.x-ctx->show_tiles_back)-0.5,(ctx->show_tiles_vertical_move-ctx->mouse_y/ctx->show_tiles_size));
			}


			switch (event.type) {
				case SDL_QUIT:
					ctx->quit = true;
					break;

				case SDL_KEYDOWN:
					if (event.key.keysym.sym == SDLK_ESCAPE)
					ctx->quit = true;
					break;
				default:
					break;
			}
		}

		// clear screen
		SDL_SetRenderDrawColor(ctx->ren, 0, 0, 0, 255);
		SDL_RenderClear(ctx->ren);

		SDL_SetRenderDrawColor(ctx->ren, 0x99, 0x99, 0x99, 255);

		for(int j=1;j<12;j++)
		{
				SDL_Rect rect={0,(ctx->show_tiles_vertical_move-j)*ctx->show_tiles_size,ctx->show_tiles_size,ctx->show_tiles_size};
				rect.h=ctx->show_tiles_max_height;
				rect.y+=ctx->show_tiles_size;
				SDL_RenderDrawLine(ctx->ren,0,rect.y,ctx->win_width,rect.y);
		}

		// render visible area
		for(int i=ctx->world.player.x-ctx->show_tiles_back;i<ctx->world.player.x+ctx->show_tiles_front;i++)
		{
			int tile_col=i-ctx->world.player.x+ctx->show_tiles_back;
			if(i<0||i>=ctx->world.bricks.size())continue;

		SDL_SetRenderDrawColor(ctx->ren, 0x99, 0x99, 0x99, 255);
			SDL_RenderDrawLine(ctx->ren,(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),ctx->win_height,(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),0);


			//coins
			SDL_Rect rect={(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),(ctx->show_tiles_vertical_move-ctx->world.coins_pos[i])*ctx->show_tiles_size,ctx->show_tiles_size,ctx->show_tiles_size};
			SDL_RenderCopy(ctx->ren,ctx->textures[2],NULL,&rect);




			for(int j=0;j<ctx->world.bricks[i].size();j++)
			{
				SDL_Rect rect={(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),(ctx->show_tiles_vertical_move-ctx->world.bricks[i][j].altitude)*ctx->show_tiles_size,ctx->show_tiles_size,ctx->show_tiles_size};
					
				switch(ctx->world.bricks[i][j].type)
				{
					case 0:
						SDL_RenderCopy(ctx->ren,ctx->textures[4],NULL,&rect);
						SDL_SetRenderDrawColor(ctx->ren, 150+19*(i%2),115,70,  255);
						rect.h=ctx->show_tiles_max_height;
						rect.y+=ctx->show_tiles_size;
						SDL_RenderFillRect(ctx->ren,&rect);
						break;
					case 1:
						SDL_RenderCopy(ctx->ren,ctx->textures[4],NULL,&rect);
						break;
					case 2:
						SDL_RenderCopy(ctx->ren,ctx->textures[5],NULL,&rect);
						break;
					case 3:
						SDL_RenderCopy(ctx->ren,ctx->textures[6],NULL,&rect);
						break;
					case 4:
						SDL_RenderCopy(ctx->ren,ctx->textures[7],NULL,&rect);
						break;
				}
						

				
			}
			

			// water level
			SDL_SetRenderDrawColor(ctx->ren, 100*(i%2),100*(i%2),255,  255);
			SDL_Rect rect2={(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),(int)(ctx->show_tiles_vertical_move*ctx->show_tiles_size-ctx->show_tiles_water_level*ctx->show_tiles_size+sin(ctx->frameTime/1000.0+i)*ctx->show_tiles_size*0.33),ctx->show_tiles_size,ctx->show_tiles_max_height};
			SDL_RenderFillRect(ctx->ren,&rect2);
		}

		// render player char
		SDL_Rect rect={(int)((ctx->show_tiles_back-0.5)*ctx->show_tiles_size),(int)((ctx->show_tiles_vertical_move-ctx->world.player.y)*ctx->show_tiles_size-ctx->world.player.y2*ctx->show_tiles_size),ctx->show_tiles_size,ctx->show_tiles_size};
		if(ctx->world.player.anim==0)SDL_RenderCopy(ctx->ren,ctx->textures[0],NULL,&rect);
		else SDL_RenderCopy(ctx->ren,ctx->textures[1],NULL,&rect);

		// HUD
		std::stringstream strbuffer;
		strbuffer << "NEXT_STONE (see below)"<<  " | SCORE:" << ctx->world.coins << " | LVL:" << ctx->world.level <<  " | FPS:" << ctx->fps << " | RES:"<<ctx->win_width << "*" << ctx->win_height ;
		sdl_put_str(ctx->ren, ctx->textures[3],10, 10, 10,strbuffer.str(),222,255,222,60,ctx->win_width,ctx->win_height,1,1);

		rect={10,20,60,60};
					
				switch(ctx->world.next_stone)
				{
					case 2:
						SDL_RenderCopy(ctx->ren,ctx->textures[5],NULL,&rect);
						break;
					case 3:
						SDL_RenderCopy(ctx->ren,ctx->textures[6],NULL,&rect);
						break;
					case 4:
						SDL_RenderCopy(ctx->ren,ctx->textures[7],NULL,&rect);
						break;
				}
		//show map !?
#ifndef __EMSCRIPTEN__
		rect={100,20,150,150};
		SDL_RenderCopy(ctx->ren,ctx->textures[8],NULL,&rect);
				
#endif
		if(ctx->world.player.dead)
		{
			sdl_put_str(ctx->ren, ctx->textures[3],300, -1, 10,"LUNATIC LEMMY DIED!",250,100,100,19,ctx->win_width,ctx->win_height,1,2);
			sdl_put_str(ctx->ren, ctx->textures[3],280, -1, 5,"[click to retry]",200,190,222,15,ctx->win_width,ctx->win_height,2,2);
		}

		if(ctx->world.player.win)
		{
			sdl_put_str(ctx->ren, ctx->textures[3],300, -1, 10,"    ! LEVEL UP       ",100,250,100,22,ctx->win_width,ctx->win_height,1,2);
			sdl_put_str(ctx->ren, ctx->textures[3],280, -1, 5,"[click for next level]",100,250,222,22,ctx->win_width,ctx->win_height,2,2);
		}
		//
	
		// finish rendering
		SDL_RenderPresent(ctx->ren);
		if(ctx->world.player.collected_coin)
		{	
			ctx->world.player.collected_coin=false;
			sample=0;
		}

		// progress world simu 
		ctx->world.sim(ctx->deltaTime);

#ifdef __EMSCRIPTEN
	if(ctx->quit)emscripten_cancel_main_loop();
#endif

}

int main(int, char**){

	Context ctx1;
	Context *ctx=&ctx1;

	// init SDL and assign SDL_Renderer on success
	ctx->sdl_init=sdl_start(ctx->win_width,ctx->win_height);

	if(ctx->sdl_init.first!=0)
	{	
		sdl_stop(ctx->sdl_init);
		return ctx->sdl_init.first;
	}

	ctx->ren=ctx->sdl_init.second.second;

	// init Textures
	ctx->textures.push_back(sdl_load_texture("assets/guy01.bmp",0,0,255,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/guy02.bmp",0,0,255,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/coin.bmp",255,255,255,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/fonts.bmp",0,0,0,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/earth01.bmp",0,0,0,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/gridder01.bmp",255,255,255,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/gridder02.bmp",255,255,255,ctx->ren));
	ctx->textures.push_back(sdl_load_texture("assets/gridder03.bmp",255,255,255,ctx->ren));
// PERLIN GENEARTED MAP
#ifndef __EMSCRIPTEN__
        ctx->textures.push_back(SDL_CreateTexture(ctx->ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 1024, 768));

	double seed=22;
	PerlinNoise perlin(seed);

	SDL_SetRenderTarget(ctx->ren, ctx->textures[8]);
	SDL_SetRenderDrawColor(ctx->ren, 0x00, 0x00, 0x00, 0x00);
	SDL_RenderClear(ctx->ren);


//	std::random_device rd(seed);  //Will be used to obtain a seed for the random number engine
	std::mt19937 gen(seed); //Standard mersenne_twister_engine seeded with rd()
	std::uniform_int_distribution<> dis(0,50);

	for(int x=0;x<1024;x++)
		for(int y=0;y<768;y++)
		{
			//SDL_SetRenderDrawColor(ren, (0.7*perlin.noise(x/1024.0*20,y/768.0*20,0)+0.2*perlin.noise(x/1024.0*200,y/768.0*200,0))*0xFF, 0x00, 0x00, 0x00);				
			double per1=perlin.noise(x/1024.0*3,y/768.0*3,0);
			double per2=perlin.noise(x/1024.0*50,y/768.0*50,0);
			if(per1<0.4)per1+=per2*0.1;

			if(per1<0.25)SDL_SetRenderDrawColor(ctx->ren, 0x00, 0x00, 0x99, 0x00);
			else if(per1<0.4)SDL_SetRenderDrawColor(ctx->ren, 0x00, 0x33, 0xff, 0x00);
			else if(per1<0.51)SDL_SetRenderDrawColor(ctx->ren,  0x55, 0xff, 0x55,0x00);
			else if(per1<0.8)
			{
				SDL_SetRenderDrawColor(ctx->ren,  0x33, 0x99, 0x33,0x00);
			}
			else if(per1<0.85)SDL_SetRenderDrawColor(ctx->ren,  0x99, 0x99, 0x99,0x00);
			else SDL_SetRenderDrawColor(ctx->ren, 0xff, 0xff, 0xff, 0x00);				
			//else SDL_SetRenderDrawColor(ren, perlin.noise(x/1024.0*20,y/768.0*20,0)*100, 0x00, 0x00, 0x00);				
	
			SDL_RenderDrawPoint(ctx->ren,x,y);
		}
	
	for(int i=0;i<30;i++)
	{
		int x=dis(gen);
		int y=dis(gen);
		int t=dis(gen);
		SDL_Rect r;
		r.x=1024*x/50+25;
		r.y=768*y/50+25;
		double per1=perlin.noise(r.x/1024.0*3,r.y/768.0*3,0);
		double per2=perlin.noise(r.x/1024.0*50,r.y/768.0*50,0);
		if(per1<0.4)per1+=per2*0.1;
		r.w=1024/100;
		r.h=768/100;

		if(per1<0.4)
		{
			SDL_SetRenderDrawColor(ctx->ren,  0x00, 0x00, 0x00,0x00);
			SDL_RenderFillRect(ctx->ren,&r);
			sdl_put_str(ctx->ren,ctx->textures[3], r.x+5, r.y,15,"boat",255,0,0,12,0,0,0,0);
		}

		if(per1>0.51&&per1<0.8)
		{
			SDL_SetRenderDrawColor(ctx->ren,  0x00, 0x00, 0x00,0x00);
			SDL_RenderFillRect(ctx->ren,&r);
//			sdl_put_str(ren, textures[0],10, 10, 10,strbuffer.str(),222,255,222,60,win_width,win_height,1,1);
			if(t<10)sdl_put_str(ctx->ren,ctx->textures[3], r.x+5, r.y,15,"blacksmith",0,0,0,12,0,0,0,0);
			else if(t<20)sdl_put_str(ctx->ren,ctx->textures[3], r.x+5, r.y,15,"inn",255,0,255,12,0,0,0,0);
			else if(t<30)sdl_put_str(ctx->ren,ctx->textures[3], r.x+5, r.y,15,"armory",255,255,0,12,0,0,0,0);
			else if(t<40)sdl_put_str(ctx->ren,ctx->textures[3], r.x+5, r.y,15,"quest",0,255,255,12,0,0,0,0);
			else sdl_put_str(ctx->ren,ctx->textures[3], r.x+5, r.y,15,"safehouse",255,255,255,12,0,0,0,0);
		}
	}
	SDL_SetRenderTarget(ctx->ren, NULL);
#endif
// PERLIN GENEARTED MAP OVER

	
	ctx->show_tiles_size=ctx->win_height/15;
	ctx->show_tiles_back=ctx->win_width/ctx->show_tiles_size*0.25;
	ctx->show_tiles_front=ctx->win_width/ctx->show_tiles_size*1;

			sdl_play_sound();
#ifdef __EMSCRIPTEN__

    int simulate_infinite_loop = 1;
    emscripten_set_main_loop_arg(main_loop, ctx, -1, simulate_infinite_loop);

#endif

	while (!ctx->quit)
	{
		main_loop(ctx);
	}

	//Clean up and quit
	for(SDL_Texture *tex:ctx->textures)
	{
		SDL_DestroyTexture(tex);
	}

	sdl_stop(ctx->sdl_init);
	
	return 0;
}