// File: /workspace/github_c_makefile_star2_O0/FredTheDino[P]Fog/src/engine/renderer/opengl_renderer.cpp
// Function: Renderer::Impl::init(char const*, int, int)
b8 init(const char *title, int width, int height) {
    // Initalize the cameras zoom
    for (u32 i = 0; i < OPENGL_NUM_CAMERAS; i++) {
        _fog_global_window_state.cam[i].zoom = 1.0;
    }
    if (SDL_Init(SDL_INIT_EVERYTHING)) {
        ERR("Failed to initalize SDL");
        return false;
    }
    window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            width, height, SDL_WINDOW_OPENGL);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    context = SDL_GL_CreateContext(window);

    if (!gladLoadGL()) {
        ERR("Failed to load OpenGL");
        return false;
    }
    resize_window(width, height);

    SDL::window_callback = resize_window;
    SDL_GL_SetSwapInterval(1);
    glEnable(GL_DEBUG_OUTPUT);
#if FOG_VERBOSE
    glDebugMessageCallback(gl_debug_message, 0);
#endif

    for (u32 i = 0; i < OPENGL_NUM_LAYERS; i++) {
        sprite_render_queues[i].create(512);
    }
    font_render_queue.create(256);

    // Initalize texture indicies
    for (u32 i = 0; i < OPENGL_NUM_CAMERAS; i++)
        _fog_texture_indicies[i] = i;

    glGenBuffers(1, &ubo_global);
    glBindBuffer(GL_UNIFORM_BUFFER, ubo_global);
    glBufferData(GL_UNIFORM_BUFFER, ubo_global_size, NULL, GL_STREAM_DRAW);
    glBindBufferBase(GL_UNIFORM_BUFFER, GLSL_GLOBAL_BLOCK, ubo_global);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);

    create_frame_buffers(width, height);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glGenTextures(1, &sprite_texture_array);
    glBindTexture(GL_TEXTURE_2D_ARRAY, sprite_texture_array);

    glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, OPENGL_TEXTURE_WIDTH,
                   OPENGL_TEXTURE_HEIGHT, OPENGL_TEXTURE_DEPTH);

    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glActiveTexture(GL_TEXTURE0);

    // Set initial state
    glClearColor(0.3f, 0.1f, 0.2f, 1.0f);
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x2, -0x1c(%rbp)
jae 0x2bd9a
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x59501(%rip), %rax     # 0x8527c
shlq $0x5, %rcx
addq %rcx, %rax
movss 0x1927e(%rip), %xmm0    # 0x45008
movss %xmm0, 0x10(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2bd69
movl $0x7231, %edi           # imm = 0x7231
callq 0x2530
cmpl $0x0, %eax
je 0x2bde4
movq 0x3d230(%rip), %rax     # 0x68fe0
movq (%rax), %rsi
leaq 0x19eb5(%rip), %rdi     # 0x45c6f
leaq 0x1a284(%rip), %rdx     # 0x46045
movl $0x158, %ecx            # imm = 0x158
leaq 0x19dea(%rip), %r8      # 0x45bb7
leaq 0x1a2c5(%rip), %r9      # 0x46099
movb $0x0, %al
callq 0xb100
movb $0x0, -0x1(%rbp)
jmp 0x2c0cc
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
movl $0x2, %r9d
movl %edx, %esi
callq 0x2260
movq %rax, %rcx
leaq 0x59455(%rip), %rax     # 0x85260
movq %rcx, (%rax)
movl $0x11, %edi
movl $0x3, %esi
callq 0x2490
movl $0x12, %edi
movl $0x3, %esi
callq 0x2490
movl $0x15, %edi
movl $0x1, %esi
callq 0x2490
leaq 0x5941e(%rip), %rax     # 0x85260
movq (%rax), %rdi
callq 0x2360
movq %rax, %rcx
leaq 0x59414(%rip), %rax     # 0x85268
movq %rcx, (%rax)
callq 0x11ee0
cmpl $0x0, %eax
jne 0x2be9c
movq 0x3d178(%rip), %rax     # 0x68fe0
movq (%rax), %rsi
leaq 0x19dfd(%rip), %rdi     # 0x45c6f
leaq 0x1a1cc(%rip), %rdx     # 0x46045
movl $0x165, %ecx            # imm = 0x165
leaq 0x19d32(%rip), %r8      # 0x45bb7
leaq 0x1a225(%rip), %r9      # 0x460b1
movb $0x0, %al
callq 0xb100
movb $0x0, -0x1(%rbp)
jmp 0x2c0cc
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x2b6a0
leaq 0x3ea92(%rip), %rax     # 0x6a940
leaq -0x815(%rip), %rcx      # 0x2b6a0
movq %rcx, (%rax)
movl $0x1, %edi
callq 0x22d0
movl $0x92e0, %edi           # imm = 0x92E0
leaq 0x52f9a(%rip), %rax     # 0x7ee68
callq *(%rax)
movl $0x0, -0x20(%rbp)
cmpl $0x10, -0x20(%rbp)
jae 0x2bf03
movl -0x20(%rbp), %eax
leaq 0x59129(%rip), %rdi     # 0x85010
shlq $0x5, %rax
addq %rax, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x41a40
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2bed7
leaq 0x59306(%rip), %rdi     # 0x85210
movl $0x100, %esi            # imm = 0x100
callq 0x41ae0
movl $0x0, -0x24(%rbp)
cmpl $0x2, -0x24(%rbp)
jae 0x2bf3e
movl -0x24(%rbp), %edx
movl -0x24(%rbp), %eax
movl %eax, %ecx
leaq 0x59344(%rip), %rax     # 0x85274
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2bf1b
movl $0x1, %edi
leaq 0x59382(%rip), %rsi     # 0x852cc
leaq 0x5306f(%rip), %rax     # 0x7efc0
callq *(%rax)
leaq 0x52b56(%rip), %rax     # 0x7eab0
movq (%rax), %rax
movl 0x59369(%rip), %esi     # 0x852cc
movl $0x8a11, %edi           # imm = 0x8A11
callq *%rax
movl $0x8a11, %edi           # imm = 0x8A11
movl $0x50, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0x88e0, %ecx           # imm = 0x88E0
leaq 0x52ba4(%rip), %rax     # 0x7eb28
callq *(%rax)
leaq 0x52b2b(%rip), %rax     # 0x7eab8
movq (%rax), %rax
movl 0x59336(%rip), %edx     # 0x852cc
movl $0x8a11, %edi           # imm = 0x8A11
xorl %esi, %esi
callq *%rax
movl $0x8a11, %edi           # imm = 0x8A11
xorl %esi, %esi
leaq 0x52b03(%rip), %rax     # 0x7eab0
callq *(%rax)
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x2b850
movl $0xbe2, %edi            # imm = 0xBE2
leaq 0x52ea2(%rip), %rax     # 0x7ee68
callq *(%rax)
movl $0x302, %edi            # imm = 0x302
movl $0x303, %esi            # imm = 0x303
leaq 0x52b37(%rip), %rax     # 0x7eb10
callq *(%rax)
movl $0x1, %edi
leaq 0x59249(%rip), %rsi     # 0x85230
leaq 0x52ffa(%rip), %rax     # 0x7efe8
callq *(%rax)
leaq 0x52ae9(%rip), %rax     # 0x7eae0
movq (%rax), %rax
leaq 0x5922f(%rip), %rcx     # 0x85230
movl (%rcx), %esi
movl $0x8c1a, %edi           # imm = 0x8C1A
callq *%rax
movl $0x8c1a, %edi           # imm = 0x8C1A
movl $0x1, %esi
movl $0x8058, %edx           # imm = 0x8058
movl $0x200, %r8d            # imm = 0x200
movl $0x100, %r9d            # imm = 0x100
leaq 0x55bd4(%rip), %rax     # 0x81c00
movl %r8d, %ecx
callq *(%rax)
movl $0x8c1a, %edi           # imm = 0x8C1A
movl $0x2801, %esi           # imm = 0x2801
movl $0x2601, %edx           # imm = 0x2601
leaq 0x53a51(%rip), %rax     # 0x7fa98
callq *(%rax)
movl $0x8c1a, %edi           # imm = 0x8C1A
movl $0x2800, %esi           # imm = 0x2800
movl $0x2601, %edx           # imm = 0x2601
leaq 0x53a39(%rip), %rax     # 0x7fa98
callq *(%rax)
movl $0x8c1a, %edi           # imm = 0x8C1A
movl $0x2802, %esi           # imm = 0x2802
movl $0x812f, %edx           # imm = 0x812F
leaq 0x53a21(%rip), %rax     # 0x7fa98
callq *(%rax)
movl $0x8c1a, %edi           # imm = 0x8C1A
movl $0x2803, %esi           # imm = 0x2803
movl $0x812f, %edx           # imm = 0x812F
leaq 0x53a09(%rip), %rax     # 0x7fa98
callq *(%rax)
movl $0x84c0, %edi           # imm = 0x84C0
leaq 0x529c3(%rip), %rax     # 0x7ea60
callq *(%rax)
movss 0x19179(%rip), %xmm0    # 0x45220
movss 0x19165(%rip), %xmm1    # 0x45214
movss 0x18f55(%rip), %xmm2    # 0x4500c
movss 0x18f49(%rip), %xmm3    # 0x45008
leaq 0x52ac2(%rip), %rax     # 0x7eb88
callq *(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)