// File: /workspace/github_c_makefile_star2_O0/aap[P]rx-0/misc/crt.c
// Function: renderthread
static void*
renderthread(void *arg)
{
	CRT *crt;
	SDL_Event ev;
	SDL_MouseButtonEvent *mbev;
	SDL_MouseMotionEvent *mmev;
	SDL_Texture *tex;

	crt = arg;

	crtinit();

	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1");

	joyinit();

	if(SDL_CreateWindowAndRenderer(winsize, winsize, 0, &window, &renderer) < 0)
		err("SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
	tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
		SDL_TEXTUREACCESS_STREAMING, 1024, 1024);
	SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
	texrect.x = 0;
	texrect.y = 0;
	texrect.w = winsize;
	texrect.h = winsize;

	SDL_ShowCursor(SDL_DISABLE);

	for(;;){
		while(SDL_PollEvent(&ev))
			switch(ev.type){
			case SDL_WINDOWEVENT:
				winev(&ev.window);
				break;

			case SDL_KEYDOWN:
				keydn(ev.key.keysym.scancode);
//				if(ev.key.keysym.scancode == SDL_SCANCODE_Q)
//					exit(0);
				break;
			case SDL_KEYUP:
				keyup(ev.key.keysym.scancode);
				break;

			case SDL_JOYBUTTONDOWN:
			case SDL_JOYBUTTONUP:
				joybtn(&ev.jbutton);
				break;
			case SDL_JOYHATMOTION:
				joyhat(&ev.jhat);
				break;
			case SDL_JOYDEVICEADDED:
				// ev.jdevice SDL_JoyDeviceEvent
				break;
			case SDL_JOYDEVICEREMOVED:
				break;

			case SDL_MOUSEMOTION:
				mmev = (SDL_MouseMotionEvent*)&ev;
				mouse(0, mmev->state, mmev->x, mmev->y);
				break;
			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				mbev = (SDL_MouseButtonEvent*)&ev;
				mouse(mbev->button, mbev->state, mbev->x, mbev->y);
				break;
			}
		SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
		SDL_RenderClear(renderer);

#ifdef AGE
		if(age >= AGETHRESH){
			lock(&crtlock);
			age -= AGETHRESH;
			unlock(&crtlock);
			render(crt);
			SDL_UpdateTexture(tex, nil, crt->pixels, 1024*sizeof(u32));
		}
#else
		render(crt);
		SDL_UpdateTexture(tex, nil, crt->pixels, 1024*sizeof(u32));
		SDL_Delay(30);
#endif

		SDL_RenderCopy(renderer, tex, nil, &texrect);
	//	SDL_RenderCopy(renderer, tex, nil, nil);

		SDL_RenderPresent(renderer);
	}
	return nil;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
callq 0x3690
movl $0xf231, %edi           # imm = 0xF231
callq 0x2230
leaq 0x308c(%rip), %rdi      # 0x6226
leaq 0x30aa(%rip), %rsi      # 0x624b
callq 0x2420
callq 0x2dc0
movl 0x50af(%rip), %edi      # 0x8260
movl 0x50a9(%rip), %esi      # 0x8260
xorl %edx, %edx
leaq 0x5118(%rip), %rcx      # 0x82d8
leaq 0x1d051d1(%rip), %r8    # 0x1d08398
callq 0x2100
cmpl $0x0, %eax
jge 0x31e7
callq 0x21a0
movq %rax, %rsi
leaq 0x306d(%rip), %rdi      # 0x624d
movb $0x0, %al
callq 0x25a0
movq 0x1d051aa(%rip), %rdi   # 0x1d08398
movl $0x16462004, %esi       # imm = 0x16462004
movl $0x1, %edx
movl $0x400, %r8d            # imm = 0x400
movl %r8d, %ecx
callq 0x2460
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movl $0x1, %esi
callq 0x22a0
movl $0x0, 0x50be(%rip)      # 0x82e0
movl $0x0, 0x50b8(%rip)      # 0x82e4
movl 0x502e(%rip), %eax      # 0x8260
movl %eax, 0x50b0(%rip)      # 0x82e8
movl 0x5022(%rip), %eax      # 0x8260
movl %eax, 0x50a8(%rip)      # 0x82ec
xorl %edi, %edi
callq 0x2490
jmp 0x324d
leaq -0x48(%rbp), %rdi
callq 0x2160
cmpl $0x0, %eax
je 0x336e
movl -0x48(%rbp), %eax
movl %eax, -0x64(%rbp)
subl $0x200, %eax            # imm = 0x200
je 0x32df
jmp 0x326e
movl -0x64(%rbp), %eax
subl $0x300, %eax            # imm = 0x300
je 0x32ea
jmp 0x327a
movl -0x64(%rbp), %eax
subl $0x301, %eax            # imm = 0x301
je 0x32f4
jmp 0x3286
movl -0x64(%rbp), %eax
subl $0x400, %eax            # imm = 0x400
je 0x3318
jmp 0x3296
movl -0x64(%rbp), %eax
addl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
subl $0x2, %eax
jb 0x333e
jmp 0x32a9
movl -0x64(%rbp), %eax
subl $0x602, %eax            # imm = 0x602
je 0x3309
jmp 0x32b5
movl -0x64(%rbp), %eax
addl $0xfffff9fd, %eax       # imm = 0xFFFFF9FD
subl $0x2, %eax
jb 0x32fe
jmp 0x32c4
movl -0x64(%rbp), %eax
subl $0x605, %eax            # imm = 0x605
je 0x3314
jmp 0x32d0
movl -0x64(%rbp), %eax
subl $0x606, %eax            # imm = 0x606
je 0x3316
jmp 0x3369
leaq -0x48(%rbp), %rdi
callq 0x2f80
jmp 0x3369
movl -0x38(%rbp), %edi
callq 0x2750
jmp 0x3369
movl -0x38(%rbp), %edi
callq 0x2960
jmp 0x3369
leaq -0x48(%rbp), %rdi
callq 0x2d00
jmp 0x3369
leaq -0x48(%rbp), %rdi
callq 0x2c10
jmp 0x3369
jmp 0x3369
jmp 0x3369
leaq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl 0x10(%rax), %esi
movq -0x58(%rbp), %rax
movl 0x14(%rax), %edx
movq -0x58(%rbp), %rax
movl 0x18(%rax), %ecx
xorl %edi, %edi
callq 0x2ee0
jmp 0x3369
leaq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movzbl 0x10(%rax), %edi
movq -0x50(%rbp), %rax
movzbl 0x11(%rax), %esi
movq -0x50(%rbp), %rax
movl 0x14(%rax), %edx
movq -0x50(%rbp), %rax
movl 0x18(%rax), %ecx
callq 0x2ee0
jmp 0x324d
movq 0x1d05023(%rip), %rdi   # 0x1d08398
xorl %r8d, %r8d
movl %r8d, %esi
movl %r8d, %edx
movl %r8d, %ecx
callq 0x2350
movq 0x1d0500b(%rip), %rdi   # 0x1d08398
callq 0x2250
movq -0x10(%rbp), %rdi
callq 0x3c10
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x1900014, %rdx        # imm = 0x1900014
xorl %eax, %eax
movl %eax, %esi
movl $0x1000, %ecx           # imm = 0x1000
callq 0x21b0
movl $0x1e, %edi
callq 0x2190
movq 0x1d04fcf(%rip), %rdi   # 0x1d08398
movq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq 0x4f08(%rip), %rcx      # 0x82e0
callq 0x2270
movq 0x1d04fb4(%rip), %rdi   # 0x1d08398
callq 0x2080
jmp 0x324b
nop