// File: khedoros[P]khednes/compositor.cpp
// Function: compositor::compositor(int)
compositor::compositor(int res) {
    /* Initialize the SDL library */
    screen = SDL_CreateWindow("KhedNES",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              320, 240,
                              SDL_WINDOW_RESIZABLE);
    if ( screen == NULL ) {
        fprintf(stderr, "Couldn't set 320x240x8 video mode: %s\nStarting without video output.\n",
                SDL_GetError());
        //exit(1);
        dummy = true;
        return;
    }

    dummy = false;

    SDL_SetWindowMinimumSize(screen, 320, 240);
    cur_x_res=256;
    cur_y_res=240;

    render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED/*|SDL_RENDERER_PRESENTVSYNC*/);
    //render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
    //render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_PRESENTVSYNC);
    //render = SDL_CreateRenderer(screen, -1, SDL_RENDERER_SOFTWARE/*|SDL_RENDERER_PRESENTVSYNC*/);
    texture = SDL_CreateTexture(render, SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,256,240);

    buffer = SDL_CreateRGBSurface(0,256,240,8,0,0,0,0);
    overlay = SDL_CreateRGBSurface(0,256,240,8,0,0,0,0);
    lps = SDL_CreateRGBSurface(0,256,240,8,0,0,0,0);
    hps = SDL_CreateRGBSurface(0,256,240,8,0,0,0,0);
    nt = SDL_CreateRGBSurface(0,768,720,8,0,0,0,0);

    SDL_SetRenderDrawColor(render, 0, 0, 0, 0);
    SDL_RenderClear(render);
    SDL_RenderPresent(render);

    for(int i=0;i<76;++i) {
        tv_palette[i].r=palette_table[i*3];
        tv_palette[i].g=palette_table[i*3+1];
        tv_palette[i].b=palette_table[i*3+2];
        if(i==NES_TRANSPARENT)
            tv_palette[i].a = 0;
        else
            tv_palette[i].a=255;
    }

    palstruct = SDL_AllocPalette(256);
    SDL_SetPaletteColors(palstruct,&tv_palette[0],0,76);

    SDL_SetSurfacePalette(buffer, palstruct);
    SDL_SetSurfacePalette(overlay, palstruct);
    SDL_SetSurfacePalette(lps, palstruct);
    SDL_SetSurfacePalette(hps, palstruct);
    SDL_SetSurfacePalette(nt, palstruct);

    SDL_SetColorKey(buffer, SDL_TRUE, NES_TRANSPARENT);
    SDL_SetColorKey(overlay, SDL_TRUE, NES_TRANSPARENT);
    SDL_SetColorKey(lps, SDL_TRUE, NES_TRANSPARENT);
    SDL_SetColorKey(hps, SDL_TRUE, NES_TRANSPARENT);
    SDL_SetColorKey(nt, SDL_TRUE, NES_TRANSPARENT);

    tv=false; //start with table_view deactivated
    delay = false;

}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movl %esi, 0x8c(%rsp)
movq 0x90(%rsp), %rdi
movq %rdi, 0x60(%rsp)
addq $0x190, %rdi            # imm = 0x190
movq %rdi, 0x68(%rsp)
callq 0x262b0
leaq 0x7fa5(%rip), %rdi      # 0x2d120
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
movl $0x140, %ecx            # imm = 0x140
movl $0xf0, %r8d
movl $0x20, %r9d
movl %edx, %esi
callq 0x4060
movq %rax, 0x70(%rsp)
jmp 0x2519f
movq 0x60(%rsp), %rax
movq 0x70(%rsp), %rcx
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x25216
movq 0x11dff(%rip), %rax     # 0x36fb8
movq (%rax), %rax
movq %rax, 0x50(%rsp)
callq 0x42e0
movq %rax, 0x58(%rsp)
jmp 0x251cd
movq 0x58(%rsp), %rdx
movq 0x50(%rsp), %rdi
leaq 0x7f4a(%rip), %rsi      # 0x2d128
movb $0x0, %al
callq 0x4570
movq 0x60(%rsp), %rax
movb $0x1, 0x1a8(%rax)
jmp 0x2561b
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
callq 0x262c0
jmp 0x25623
movq 0x60(%rsp), %rax
movb $0x0, 0x1a8(%rax)
movq (%rax), %rdi
movl $0x140, %esi            # imm = 0x140
movl $0xf0, %edx
callq 0x46e0
jmp 0x25236
movq 0x60(%rsp), %rax
movl $0x100, 0x178(%rax)     # imm = 0x100
movl $0xf0, 0x17c(%rax)
movq (%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x4030
movq %rax, 0x48(%rsp)
jmp 0x25268
movq 0x60(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rax), %rdi
movl $0x16362004, %esi       # imm = 0x16362004
movl $0x1, %edx
movl $0x100, %ecx            # imm = 0x100
movl $0xf0, %r8d
callq 0x46c0
movq %rax, 0x40(%rsp)
jmp 0x2529b
movq 0x60(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
movl $0x100, %esi            # imm = 0x100
movl $0xf0, %edx
movl $0x8, %ecx
xorl %r9d, %r9d
movl %r9d, %edi
movl %r9d, %r8d
callq 0x4540
movq %rax, 0x38(%rsp)
jmp 0x252dd
movq 0x60(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
movl $0x100, %esi            # imm = 0x100
movl $0xf0, %edx
movl $0x8, %ecx
xorl %r9d, %r9d
movl %r9d, %edi
movl %r9d, %r8d
callq 0x4540
movq %rax, 0x30(%rsp)
jmp 0x2531f
movq 0x60(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
movl $0x100, %esi            # imm = 0x100
movl $0xf0, %edx
movl $0x8, %ecx
xorl %r9d, %r9d
movl %r9d, %edi
movl %r9d, %r8d
callq 0x4540
movq %rax, 0x28(%rsp)
jmp 0x25361
movq 0x60(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x28(%rax)
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
movl $0x100, %esi            # imm = 0x100
movl $0xf0, %edx
movl $0x8, %ecx
xorl %r9d, %r9d
movl %r9d, %edi
movl %r9d, %r8d
callq 0x4540
movq %rax, 0x20(%rsp)
jmp 0x253a3
movq 0x60(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
movl $0x300, %esi            # imm = 0x300
movl $0x2d0, %edx            # imm = 0x2D0
movl $0x8, %ecx
xorl %r9d, %r9d
movl %r9d, %edi
movl %r9d, %r8d
callq 0x4540
movq %rax, 0x18(%rsp)
jmp 0x253e5
movq 0x60(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x8(%rax), %rdi
xorl %r8d, %r8d
movl %r8d, %esi
movl %r8d, %edx
movl %r8d, %ecx
callq 0x4560
jmp 0x2540a
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4380
jmp 0x2541a
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x40b0
jmp 0x2542a
movl $0x0, 0x78(%rsp)
cmpl $0x4c, 0x78(%rsp)
jge 0x254d8
movq 0x60(%rsp), %rax
imull $0x3, 0x78(%rsp), %ecx
movslq %ecx, %rdx
leaq 0x7d7f(%rip), %rcx      # 0x2d1d0
movl (%rcx,%rdx,4), %ecx
movb %cl, %dl
movslq 0x78(%rsp), %rcx
movb %dl, 0x40(%rax,%rcx,4)
imull $0x3, 0x78(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rdx
leaq 0x7d5f(%rip), %rcx      # 0x2d1d0
movl (%rcx,%rdx,4), %ecx
movb %cl, %dl
movslq 0x78(%rsp), %rcx
movb %dl, 0x41(%rax,%rcx,4)
imull $0x3, 0x78(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rdx
leaq 0x7d3f(%rip), %rcx      # 0x2d1d0
movl (%rcx,%rdx,4), %ecx
movb %cl, %dl
movslq 0x78(%rsp), %rcx
movb %dl, 0x42(%rax,%rcx,4)
cmpl $0x4a, 0x78(%rsp)
jne 0x254b7
movq 0x60(%rsp), %rax
movslq 0x78(%rsp), %rcx
movb $0x0, 0x43(%rax,%rcx,4)
jmp 0x254c6
movq 0x60(%rsp), %rax
movslq 0x78(%rsp), %rcx
movb $-0x1, 0x43(%rax,%rcx,4)
jmp 0x254c8
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x25432
movl $0x100, %edi            # imm = 0x100
callq 0x46d0
movq %rax, 0x10(%rsp)
jmp 0x254e9
movq 0x60(%rsp), %rsi
movq 0x10(%rsp), %rax
movq %rax, 0x170(%rsi)
movq 0x170(%rsi), %rdi
addq $0x40, %rsi
xorl %edx, %edx
movl $0x4c, %ecx
callq 0x41f0
jmp 0x25513
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
jmp 0x2552a
movq 0x60(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
jmp 0x25541
movq 0x60(%rsp), %rax
movq 0x28(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
jmp 0x25558
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
jmp 0x2556f
movq 0x60(%rsp), %rax
movq 0x30(%rax), %rdi
movq 0x170(%rax), %rsi
callq 0x41e0
jmp 0x25586
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
jmp 0x255a0
movq 0x60(%rsp), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
jmp 0x255ba
movq 0x60(%rsp), %rax
movq 0x28(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
jmp 0x255d4
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
jmp 0x255ee
movq 0x60(%rsp), %rax
movq 0x30(%rax), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
jmp 0x25608
movq 0x60(%rsp), %rax
movb $0x0, 0x188(%rax)
movb $0x0, 0x189(%rax)
addq $0x98, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x4680