// File: /workspace/github_cpp_makefile_star2_O2/khedoros[P]khednes/compositor.cpp
// Function: compositor::flip()
void compositor::flip() {
    if(!render) return;

    SDL_Rect spr_render;
    spr_render.x = spr_render.y = 0; spr_render.w = 256; spr_render.h = 240;
    SDL_Rect spr_tv;
    spr_tv.x = 512; spr_tv.y = 0; spr_tv.w = 256; spr_tv.h = 240;

    SDL_Rect nt_render;
    nt_render.x = nt_render.y = 0; nt_render.w = 256; nt_render.h = 240;
    SDL_Rect nt_tv;
    nt_tv.x = nt_tv.y = 0; nt_tv.w = 512; nt_tv.h = 480;

    SDL_Rect all;
    all.x = all.y = 0; all.w = cur_x_res; all.h = cur_y_res;

    if(!buffer) {
        cout<<"Buffer is null; not flipping nuttin'!"<<endl;
        return;
    }
    else if(!(buffer->format->palette)) {
        cout<<"Palette is null; not flipping"<<endl;
    }

    //Draw low priority sprites, then all necessary sections of the background, then high priority sprites, then the overlay
    if(!tv) {

        SDL_BlitSurface(lps,NULL,buffer,&spr_render); //Draw low-priority sprites

        int ys = 0;
        while(nt_regions.size() != 0) { //Draw each section of the background that needs drawn
            SDL_Rect * v = nt_regions.front();
            nt_regions.pop_front();
            nt_render.y = ys;
            ys += v->h;
            SDL_BlitSurface(nt,v,buffer,&nt_render);
            delete v;
        }

        SDL_BlitSurface(hps,NULL,buffer,&spr_render); //Draw the high-priority sprites

    }
    else {
        SDL_BlitSurface(lps,NULL,buffer,&spr_tv); //Draw low-priority sprites
        SDL_BlitSurface(nt,NULL,buffer,&all); //Draw entire real-address name table
        { //Draw the extra name table crap
            SDL_Rect special;
            //Top-right mirror of table 1 + 3
            special.x = 512;
            special.y = 0;
            special.w = 256;
            special.h = 480;
            SDL_BlitSurface(nt,&special,buffer,&all);

            //Bottom-right mirror of table 1
            special.y = 480;
            special.h = 240;
            SDL_BlitSurface(nt,&special,buffer,&all);

            //Bottom-left mirror of table 1 +2
            special.x = 0;
            special.w = 512;
            SDL_BlitSurface(nt,&special,buffer,&all);
        }
        
        SDL_BlitSurface(hps,NULL,buffer,&spr_tv); //Draw the high-priority sprites
        //wait(20);
    }

    SDL_BlitSurface(overlay,NULL,buffer,&all); //Put the pset'ed overlay on top

    SDL_DestroyTexture(texture);

    texture = SDL_CreateTextureFromSurface(render, buffer);

    SDL_RenderClear(render);
    SDL_RenderCopy(render,texture,NULL,NULL);
    SDL_RenderPresent(render);

    //Clear all buffers
    fill(NES_TRANSPARENT, overlay);
    fill(NES_TRANSPARENT, lps);
    fill(NES_TRANSPARENT, nt);
    fill(NES_TRANSPARENT, hps);

    //if(!delay)
        //wait(10);
}

// Assembly:
cmpq $0x0, 0x8(%rdi)
je 0x1013b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movaps 0x40f9(%rip), %xmm0     # 0x14070
movaps %xmm0, 0x30(%rsp)
movaps 0x40fd(%rip), %xmm1     # 0x14080
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, (%rsp)
movq 0x178(%rdi), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x10112
movq 0x8(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0xffd1
movq 0xaff3(%rip), %rdi      # 0x1afb0
leaq 0x4198(%rip), %rsi      # 0x1415c
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movq 0x10(%rbx), %rdx
movq 0x28(%rbx), %rdi
cmpb $0x0, 0x188(%rbx)
je 0x1013c
leaq 0x20(%rsp), %r14
xorl %esi, %esi
movq %r14, %rcx
callq 0x4300
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %rsp, %r15
xorl %esi, %esi
movq %r15, %rcx
callq 0x4300
movaps 0x407f(%rip), %xmm0     # 0x14090
leaq 0x40(%rsp), %r12
movaps %xmm0, (%r12)
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4300
movl $0x1e0, 0x4(%r12)       # imm = 0x1E0
movl $0xf0, 0xc(%r12)
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4300
andl $0x0, (%r12)
movl $0x200, 0x8(%r12)       # imm = 0x200
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4300
movq 0x10(%rbx), %rdx
movq 0x38(%rbx), %rdi
xorl %esi, %esi
movq %r14, %rcx
callq 0x4300
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rdi
movq %rsp, %rcx
xorl %esi, %esi
callq 0x4300
movq 0x18(%rbx), %rdi
callq 0x42a0
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0x44a0
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rdi
callq 0x4360
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x43d0
movq 0x8(%rbx), %rdi
callq 0x4090
movq 0x20(%rbx), %rdx
pushq $0x4a
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x101a0
movq 0x28(%rbx), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x101a0
movq 0x30(%rbx), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x101a0
movq 0x38(%rbx), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x101a0
jmp 0x1012d
movq 0xae97(%rip), %rdi      # 0x1afb0
leaq 0x4016(%rip), %rsi      # 0x14136
callq 0x4390
movq %rax, %rdi
callq 0x41c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
leaq 0x30(%rsp), %r14
xorl %esi, %esi
movq %r14, %rcx
callq 0x4300
leaq 0x190(%rbx), %r15
leaq 0x10(%rsp), %r12
cmpq $0x0, 0x1a0(%rbx)
je 0x10074
movq 0x190(%rbx), %rax
movq 0x10(%rax), %r13
movq %r15, %rdi
callq 0x1069a
movl %ebp, 0x14(%rsp)
addl 0xc(%r13), %ebp
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x4300
movq %r13, %rdi
callq 0x4370
jmp 0x10159
nop