// File: khedoros[P]khednes/compositor.cpp
// Function: compositor::resize(int, int, bool)
bool compositor::resize(int x,int y, bool maintain_aspect/*=true*/) {
    if(!screen) return true;
    if(!maintain_aspect) {
        SDL_FreeSurface(buffer);
        buffer = SDL_CreateRGBSurface(0,x,y,8,0,0,0,0);
        SDL_SetSurfacePalette(buffer, palstruct);
        SDL_SetColorKey(buffer, SDL_TRUE, NES_TRANSPARENT);

        SDL_FreeSurface(overlay);
        overlay = SDL_CreateRGBSurface(0,x,y,8,0,0,0,0);
        SDL_SetSurfacePalette(overlay, palstruct);
        SDL_SetColorKey(overlay, SDL_TRUE, NES_TRANSPARENT);

        cur_x_res=x;
        cur_y_res=y;

        SDL_SetWindowSize(screen, x, y);
        return true;
    }
    else {
        float aspect = float(base_x_res)/float(base_y_res);
        float aspect2 = float(x)/float(y);
        float aspect3 = aspect2 - aspect;
        if(aspect3 < 0) aspect3 *= -1.0;
        //if(aspect3 < 0.05 ) return false;
        int wx,wy;
        SDL_GetWindowSize(screen,&wx,&wy);
        int nx,ny;
        if(aspect < aspect2) {
            nx = int(float(y)*aspect);
            ny = y;
        }
        else {
            nx = x;
            ny = int(float(x)/aspect);
        }
        //cout<<"C-aspect: "<<aspect<<" new aspect: "<<aspect2<<" diff: "<<aspect3<<endl;
        //cout<<"Old size: ("<<wx<<", "<<wy<<"), new size: ("<<nx<<", "<<ny<<")"<<endl;
        if((nx != wx || ny != wy ) && aspect3 > 0.1) {
            SDL_SetWindowSize(screen, nx, ny);
        }
    }

    return true;
}

// Assembly:
subq $0x58, %rsp
movb %cl, %al
movq %rdi, 0x48(%rsp)
movl %esi, 0x44(%rsp)
movl %edx, 0x40(%rsp)
andb $0x1, %al
movb %al, 0x3f(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, (%rax)
jne 0x25c73
movb $0x1, 0x57(%rsp)
jmp 0x25ea4
testb $0x1, 0x3f(%rsp)
jne 0x25d89
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x4370
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
xorl %r9d, %r9d
movl $0x8, %ecx
movl %r9d, %edi
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x4540
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x4370
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
xorl %r9d, %r9d
movl $0x8, %ecx
movl %r9d, %edi
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x4540
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
movq 0x18(%rsp), %rax
movl 0x44(%rsp), %ecx
movl %ecx, 0x178(%rax)
movl 0x40(%rsp), %ecx
movl %ecx, 0x17c(%rax)
movq (%rax), %rdi
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
callq 0x4640
movb $0x1, 0x57(%rsp)
jmp 0x25ea4
movq 0x18(%rsp), %rax
cvtsi2ssl 0x180(%rax), %xmm0
cvtsi2ssl 0x184(%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0x38(%rsp)
cvtsi2ssl 0x44(%rsp), %xmm0
cvtsi2ssl 0x40(%rsp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0x34(%rsp)
movss 0x34(%rsp), %xmm0
subss 0x38(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
ucomiss 0x30(%rsp), %xmm0
jbe 0x25dfa
movss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x7324(%rip), %xmm1     # 0x2d110
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
leaq 0x2c(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x4170
movss 0x38(%rsp), %xmm1
movss 0x34(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x25e40
cvtsi2ssl 0x40(%rsp), %xmm0
mulss 0x38(%rsp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, 0x24(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x20(%rsp)
jmp 0x25e5c
movl 0x44(%rsp), %eax
movl %eax, 0x24(%rsp)
cvtsi2ssl 0x44(%rsp), %xmm0
divss 0x38(%rsp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, 0x20(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jne 0x25e70
movl 0x20(%rsp), %eax
cmpl 0x28(%rsp), %eax
je 0x25e9d
movss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x7296(%rip), %xmm1     # 0x2d118
ucomisd %xmm1, %xmm0
jbe 0x25e9d
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
callq 0x4640
jmp 0x25e9f
movb $0x1, 0x57(%rsp)
movb 0x57(%rsp), %al
andb $0x1, %al
addq $0x58, %rsp
retq
nop