// File: /workspace/github_cpp_makefile_star2_O1/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 0x12426
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movaps 0x3f3f(%rip), %xmm0     # 0x160c0
movaps %xmm0, 0x30(%rsp)
movaps 0x3f43(%rip), %xmm1     # 0x160d0
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x0, (%rsp)
movq 0x178(%rdi), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x122b0
movq 0x8(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x12205
movq 0x9de6(%rip), %r14      # 0x1bfb0
leaq 0x3fcb(%rip), %rsi      # 0x1619c
movl $0x1d, %edx
movq %r14, %rdi
callq 0x4410
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq 0x10(%rbx), %rdx
movq 0x28(%rbx), %rdi
cmpb $0x0, 0x188(%rbx)
je 0x122f7
leaq 0x20(%rsp), %r14
xorl %esi, %esi
movq %r14, %rcx
callq 0x4320
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %rsp, %r15
xorl %esi, %esi
movq %r15, %rcx
callq 0x4320
movaps 0x3e9b(%rip), %xmm0     # 0x160e0
leaq 0x40(%rsp), %r12
movaps %xmm0, (%r12)
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4320
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 0x4320
movl $0x0, (%r12)
movl $0x200, 0x8(%r12)       # imm = 0x200
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4320
jmp 0x12373
movq 0x9cf9(%rip), %rbx      # 0x1bfb0
leaq 0x3eb8(%rip), %rsi      # 0x16176
movl $0x25, %edx
movq %rbx, %rdi
callq 0x4410
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
jmp 0x12418
xorl %ebp, %ebp
leaq 0x30(%rsp), %r14
xorl %esi, %esi
movq %r14, %rcx
callq 0x4320
movq 0x1a0(%rbx), %rax
testq %rax, %rax
je 0x12373
leaq 0x10(%rsp), %r15
movq 0x190(%rbx), %r13
movq 0x10(%r13), %r12
decq %rax
movq %rax, 0x1a0(%rbx)
movq %r13, %rdi
callq 0x4590
movq %r13, %rdi
callq 0x4380
movl %ebp, 0x14(%rsp)
addl 0xc(%r12), %ebp
movq 0x10(%rbx), %rdx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x4320
testq %r12, %r12
je 0x12367
movq %r12, %rdi
callq 0x4380
movq 0x1a0(%rbx), %rax
testq %rax, %rax
jne 0x12319
movq 0x10(%rbx), %rdx
movq 0x38(%rbx), %rdi
xorl %esi, %esi
movq %r14, %rcx
callq 0x4320
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rdi
movq %rsp, %rcx
xorl %esi, %esi
callq 0x4320
movq 0x18(%rbx), %rdi
callq 0x42c0
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0x44b0
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rdi
callq 0x4370
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x43e0
movq 0x8(%rbx), %rdi
callq 0x40a0
movq 0x20(%rbx), %rdx
movq %rbx, %rdi
movl $0x4a, %esi
callq 0x12428
movq 0x28(%rbx), %rdx
movq %rbx, %rdi
movl $0x4a, %esi
callq 0x12428
movq 0x30(%rbx), %rdx
movq %rbx, %rdi
movl $0x4a, %esi
callq 0x12428
movq 0x38(%rbx), %rdx
movq %rbx, %rdi
movl $0x4a, %esi
callq 0x12428
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop