// File: /workspace/github_c_makefile_star2_O3/phoboslab[P]pl_mpeg/pl_mpeg_player_gl.c
// Function: app_update
void app_update(app_t *self) {
	double seek_to = -1;

	SDL_Event ev;
	while (SDL_PollEvent(&ev)) {
		if (
			ev.type == SDL_QUIT || 
			(ev.type == SDL_KEYUP && ev.key.keysym.sym == SDLK_ESCAPE)
		) {
			self->wants_to_quit = TRUE;
		}
		
		if (
			ev.type == SDL_WINDOWEVENT &&
			ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED
		) {
			glViewport(0, 0, ev.window.data1, ev.window.data2);
		}

		// Seek 3sec forward/backward using arrow keys
		if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_RIGHT) {
			seek_to = plm_get_time(self->plm) + 3;
		}
		else if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_LEFT) {
			seek_to = plm_get_time(self->plm) - 3;
		}
	}

	// Compute the delta time since the last app_update(), limit max step to 
	// 1/30th of a second
	double current_time = (double)SDL_GetTicks() / 1000.0;
	double elapsed_time = current_time - self->last_time;
	if (elapsed_time > 1.0 / 30.0) {
		elapsed_time = 1.0 / 30.0;
	}
	self->last_time = current_time;

	// Seek using mouse position
	int mouse_x, mouse_y;
	if (SDL_GetMouseState(&mouse_x, &mouse_y) & SDL_BUTTON(SDL_BUTTON_LEFT)) {
		int sx, sy;
		SDL_GetWindowSize(self->window, &sx, &sy);
		seek_to = plm_get_duration(self->plm) * ((float)mouse_x / (float)sx);
	}
	
	// Seek or advance decode
	if (seek_to != -1) {
		SDL_ClearQueuedAudio(self->audio_device);
		plm_seek(self->plm, seek_to, FALSE);
	}
	else {
		plm_decode(self->plm, elapsed_time);
	}

	if (plm_has_ended(self->plm)) {
		self->wants_to_quit = TRUE;
	}
	
	glClear(GL_COLOR_BUFFER_BIT);
	glRectf(0.0, 0.0, 1.0, 1.0);
	SDL_GL_SwapWindow(self->window);
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2100
movsd 0x51b(%rip), %xmm0      # 0x9230
testl %eax, %eax
je 0x8dbd
movsd %xmm0, 0x8(%rsp)
leaq 0x30(%rsp), %r14
movl 0x30(%rsp), %eax
movl 0x44(%rsp), %ecx
cmpl $0x100, %eax            # imm = 0x100
je 0x8d48
movl %eax, %edx
xorl $0x301, %edx            # imm = 0x301
movl %ecx, %esi
xorl $0x1b, %esi
orl %edx, %esi
jne 0x8d4f
movl $0x1, 0x10(%rbx)
cmpl $0x200, %eax            # imm = 0x200
jne 0x8d72
cmpb $0x6, 0x3c(%rsp)
jne 0x8d72
movl 0x40(%rsp), %edx
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
movl 0x30(%rsp), %eax
movl 0x44(%rsp), %ecx
xorl $0x300, %eax            # imm = 0x300
movl %ecx, %edx
xorl $0x4000004f, %edx       # imm = 0x4000004F
orl %eax, %edx
movsd 0x4bf(%rip), %xmm0      # 0x9248
je 0x8d9d
xorl $0x40000050, %ecx       # imm = 0x40000050
orl %ecx, %eax
movsd 0x4b5(%rip), %xmm0      # 0x9250
jne 0x8dab
movq (%rbx), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x2100
testl %eax, %eax
jne 0x8d28
jmp 0x8dc3
movsd %xmm0, 0x8(%rsp)
callq 0x2180
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x47e(%rip), %xmm0      # 0x9258
movapd %xmm0, %xmm1
subsd 0x8(%rbx), %xmm1
movsd %xmm1, 0x18(%rsp)
movsd %xmm0, 0x8(%rbx)
leaq 0x14(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x2260
testb $0x1, %al
movsd 0x8(%rsp), %xmm0
je 0x8e4b
movq 0x18(%rbx), %rdi
leaq 0x28(%rsp), %r14
leaq 0x24(%rsp), %rdx
movq %r14, %rsi
callq 0x20c0
movq (%rbx), %rax
movq (%rax), %rdi
movl $0xe0, %esi
callq 0x2fb3
xorps %xmm1, %xmm1
cvtsi2ssl 0x14(%rsp), %xmm1
cvtsi2ssl (%r14), %xmm2
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
ucomisd 0x3dd(%rip), %xmm0      # 0x9230
jne 0x8e57
jnp 0x8e77
movl 0x20(%rbx), %edi
movsd %xmm0, 0x8(%rsp)
callq 0x2250
movq (%rbx), %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
callq 0x3e39
jmp 0x8e8d
movsd 0x3e1(%rip), %xmm0      # 0x9260
minsd 0x18(%rsp), %xmm0
movq (%rbx), %rdi
callq 0x326e
movq (%rbx), %rax
cmpl $0x0, 0x10(%rax)
je 0x8e9d
movl $0x1, 0x10(%rbx)
movl $0x4000, %edi           # imm = 0x4000
callq 0x2070
movss 0x1829(%rip), %xmm2     # 0xa6d8
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movaps %xmm2, %xmm3
callq 0x2150
movq 0x18(%rbx), %rdi
callq 0x2230
addq $0x68, %rsp
popq %rbx
popq %r14
retq