// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movsd 0x7d4(%rip), %xmm0      # 0xd008
movsd %xmm0, -0x10(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2100
cmpl $0x0, %eax
je 0xc8f6
cmpl $0x100, -0x48(%rbp)     # imm = 0x100
je 0xc863
cmpl $0x301, -0x48(%rbp)     # imm = 0x301
jne 0xc86e
cmpl $0x1b, -0x34(%rbp)
jne 0xc86e
movq -0x8(%rbp), %rax
movl $0x1, 0x10(%rax)
cmpl $0x200, -0x48(%rbp)     # imm = 0x200
jne 0xc88f
movzbl -0x3c(%rbp), %eax
cmpl $0x6, %eax
jne 0xc88f
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
xorl %esi, %esi
movl %esi, %edi
callq 0x2080
cmpl $0x300, -0x48(%rbp)     # imm = 0x300
jne 0xc8c0
cmpl $0x4000004f, -0x34(%rbp) # imm = 0x4000004F
jne 0xc8c0
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x36a0
movsd 0x783(%rip), %xmm1      # 0xd038
addsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
jmp 0xc8f1
cmpl $0x300, -0x48(%rbp)     # imm = 0x300
jne 0xc8ef
cmpl $0x40000050, -0x34(%rbp) # imm = 0x40000050
jne 0xc8ef
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x36a0
movsd 0x752(%rip), %xmm1      # 0xd038
subsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
jmp 0xc8f1
jmp 0xc839
callq 0x2170
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd 0x726(%rip), %xmm1      # 0xd030
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movq -0x8(%rbp), %rax
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movsd 0x6f5(%rip), %xmm1      # 0xd028
ucomisd %xmm1, %xmm0
jbe 0xc946
movsd 0x6e7(%rip), %xmm0      # 0xd028
movsd %xmm0, -0x58(%rbp)
movsd -0x50(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x8(%rax)
leaq -0x5c(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2250
andl $0x1, %eax
cmpl $0x0, %eax
je 0xc9a5
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x64(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x20c0
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x36c0
cvtsi2ssl -0x5c(%rbp), %xmm1
cvtsi2ssl -0x64(%rbp), %xmm2
divss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x656(%rip), %xmm1      # 0xd008
ucomisd %xmm1, %xmm0
jne 0xc9bc
jp 0xc9bc
jmp 0xc9dd
movq -0x8(%rbp), %rax
movl 0x20(%rax), %edi
callq 0x2240
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movsd -0x10(%rbp), %xmm0
xorl %esi, %esi
callq 0x4c10
jmp 0xc9ee
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movsd -0x58(%rbp), %xmm0
callq 0x3ae0
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x3a60
cmpl $0x0, %eax
je 0xca0a
movq -0x8(%rbp), %rax
movl $0x1, 0x10(%rax)
movl $0x4000, %edi           # imm = 0x4000
callq 0x2070
xorps %xmm1, %xmm1
movss 0x1b41(%rip), %xmm3     # 0xe560
movaps %xmm1, %xmm0
movaps %xmm3, %xmm2
callq 0x2140
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2220
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)