// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: setgamemode
int setgamemode(char davidoption, int daxdim, int daydim, int dabpp)
{
	int i, j;

	if ((qsetmode == 200) && (videomodereset == 0) &&
	    (davidoption == fullscreen) && (xdim == daxdim) && (ydim == daydim) && (bpp == dabpp))
		return(0);

	strcpy(kensmessage,"!!!! BUILD engine&tools programmed by Ken Silverman of E.G. RI.  (c) Copyright 1995 Ken Silverman.  Summary:  BUILD = Ken. !!!!");

#if USE_POLYMOST && USE_OPENGL
	int oldbpp = bpp;
#endif
	//bytesperline is set in this function
	if (setvideomode(daxdim,daydim,dabpp,davidoption) < 0) return(-1);

#ifndef USING_A_C
	// it's possible the previous call protected our code sections again
	makeasmwriteable();
#endif

#if USE_POLYMOST && USE_OPENGL
	if (dabpp > 8) rendmode = 3;	// GL renderer
	else if (dabpp == 8 && oldbpp != 8) rendmode = 0;	// going from GL to software activates classic
#endif

	xdim = xres; ydim = yres;

	// determine the corrective factor for pixel-squareness. Build
	// is built around the non-square pixels of Mode 13h, so to get
	// things back square on VGA screens, things need to be "compressed"
	// vertically a little.
	widescreen = 0;
	tallscreen = 0;
	if ((xdim == 320 && ydim == 200) || (xdim == 640 && ydim == 400)) {
		pixelaspect = 65536;
	} else {
		int ratio = divscale16(ydim*320, xdim*240);
		pixelaspect = divscale16(240*320L,320*200L);

		if (ratio < 65536) {
			widescreen = 1;
		} else if (ratio > 65536) {
			tallscreen = 1;

			// let tall screens (eg. 1280x1024) stretch the 2D elements
			// vertically a little until something better is thought of
			pixelaspect = divscale16(ydim*320L,xdim*200L);
		}
	}

	j = ydim*4*sizeof(int);  //Leave room for horizlookup&horizlookup2

	if (lookups != NULL) { kfree((void *)lookups); lookups = NULL; }
	if ((lookups = kmalloc(j<<1)) == NULL) {
		engineerrstr = "Failed to allocate lookups memory";
		return -1;
	}

	horizlookup = (int *)(lookups);
	horizlookup2 = (int *)((intptr_t)lookups+j);
	horizycent = ((ydim*4)>>1);

	//Force drawrooms to call dosetaspect & recalculate stuff
	oxyaspect = oxdimen = oviewingrange = -1;

	setvlinebpl(bytesperline);
	j = 0;
	for(i=0;i<=ydim;i++) ylookup[i] = j, j += bytesperline;

	setview(0L,0L,xdim-1,ydim-1);

#if USE_POLYMOST && USE_OPENGL
	if (rendmode == 3) {
		polymost_glreset();
		polymost_glinit();
	}
#endif

	setbrightness(curbrightness,&palette[0],0);
	clearallviews(0L);

	if (searchx < 0) { searchx = halfxdimen; searchy = (ydimen>>1); }

	qsetmode = 200;

	//memset(ratelimitlast,0,sizeof(ratelimitlast));
	//ratelimitn = 0;

	return(0);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %ecx, %ebp
movl %edi, %ecx
cmpl $0xc8, 0x180b7e(%rip)   # 0x1b1a28
leaq 0x1461f3(%rip), %rax    # 0x1770a4
movl (%rax), %r14d
jne 0x30ef5
leaq 0x1461fc(%rip), %rax    # 0x1770b9
cmpb $0x0, (%rax)
jne 0x30ef5
leaq 0x1461df(%rip), %rax    # 0x1770a8
movl (%rax), %eax
xorl %ecx, %eax
movl 0x3d0119(%rip), %edi    # 0x400fec
xorl %esi, %edi
movl 0x3d0114(%rip), %r8d    # 0x400ff0
xorl %edx, %r8d
orl %edi, %r8d
orl %eax, %r8d
movl %r14d, %eax
xorl %ebp, %eax
xorl %ebx, %ebx
orl %r8d, %eax
je 0x310b0
movl %esi, %edi
movl %edx, %esi
movl %ebp, %edx
callq 0x63081
pushq $-0x1
popq %rbx
testl %eax, %eax
js 0x310b0
cmpl $0x8, %ebp
jle 0x30f15
pushq $0x3
popq %rax
jmp 0x30f28
sete %al
cmpl $0x8, %r14d
setne %cl
andb %al, %cl
xorl %eax, %eax
cmpb $0x1, %cl
jne 0x30f31
leaq 0x5c2a21(%rip), %rcx    # 0x5f3950
movl %eax, (%rcx)
leaq 0x6c0f8(%rip), %rax     # 0x9d030
movslq (%rax), %rsi
movl %esi, 0x3d00ab(%rip)    # 0x400fec
leaq 0x6c0ec(%rip), %rax     # 0x9d034
movl (%rax), %ebp
movl %ebp, 0x3d00a0(%rip)    # 0x400ff0
andl $0x0, 0x3ded19(%rip)    # 0x40fc70
andl $0x0, 0x3ded16(%rip)    # 0x40fc74
movl %esi, %eax
xorl $0x140, %eax            # imm = 0x140
movl %ebp, %ecx
xorl $0xc8, %ecx
orl %eax, %ecx
je 0x30f8c
movslq %ebp, %rax
movl %esi, %ecx
xorl $0x280, %ecx            # imm = 0x280
movl %eax, %edx
xorl $0x190, %edx            # imm = 0x190
orl %ecx, %edx
jne 0x310b7
movl $0x10000, 0x3dece2(%rip) # imm = 0x10000
movq 0x374563(%rip), %rdi    # 0x3a5500
callq 0x61c0
movl %ebp, %eax
shll $0x5, %eax
movslq %eax, %rdi
callq 0x69a0
movq %rax, 0x37454a(%rip)    # 0x3a5500
testq %rax, %rax
je 0x310a2
shll $0x4, %ebp
movq %rax, 0x3decb7(%rip)    # 0x40fc80
movslq %ebp, %rcx
addq %rax, %rcx
orl $-0x1, 0x6cc7a(%rip)    # 0x9dc50
movq %rcx, 0x3decab(%rip)    # 0x40fc88
orl $-0x1, 0x6cc68(%rip)    # 0x9dc4c
movl 0x3d0006(%rip), %eax    # 0x400ff0
orl $-0x1, 0x6cc57(%rip)    # 0x9dc48
addl %eax, %eax
movl %eax, 0x3dec97(%rip)    # 0x40fc90
leaq 0x146284(%rip), %rbx    # 0x177284
movl (%rbx), %edi
callq 0x64198
movslq 0x3cffe2(%rip), %rcx    # 0x400ff0
movl (%rbx), %eax
leaq 0x3dec89(%rip), %rdx    # 0x40fca0
xorl %esi, %esi
xorl %edi, %edi
cmpq %rcx, %rdi
jg 0x3102d
movl %esi, (%rdx)
incq %rdi
addq $0x4, %rdx
addl %eax, %esi
jmp 0x3101b
movl 0x3cffb9(%rip), %edx    # 0x400fec
decl %edx
decl %ecx
xorl %edi, %edi
xorl %esi, %esi
callq 0x3111e
leaq 0x5c2909(%rip), %rax    # 0x5f3950
cmpl $0x3, (%rax)
jne 0x31056
callq 0x46917
callq 0x46a9c
movl 0x1809a8(%rip), %edi    # 0x1b1a04
leaq 0x24454d(%rip), %rsi    # 0x2755b0
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x31279
xorl %edi, %edi
callq 0x314ca
cmpl $0x0, 0x6cbc6(%rip)     # 0x9dc40
jns 0x31096
movl 0x3e0de2(%rip), %eax    # 0x411e64
movl %eax, 0x6cbb8(%rip)     # 0x9dc40
movl 0x243a06(%rip), %eax    # 0x274a94
sarl %eax
movl %eax, 0x3e0dd2(%rip)    # 0x411e68
movl $0xc8, 0x180988(%rip)   # 0x1b1a28
jmp 0x310b0
leaq 0x3b2ad(%rip), %rax     # 0x6c356
movq %rax, 0x180970(%rip)    # 0x1b1a20
movl %ebx, %eax
popq %rbx
popq %r14
popq %rbp
retq
imulq $0xf0, %rsi, %rdi
imulq $0x1400000, %rax, %rcx  # imm = 0x1400000
movq %rcx, %rax
cqto
idivq %rdi
movl $0x13333, 0x3deba1(%rip) # imm = 0x13333
cmpl $0xffff, %eax           # imm = 0xFFFF
jg 0x310ed
movl $0x1, 0x3deb88(%rip)    # 0x40fc70
jmp 0x30f96
cmpl $0x10000, %eax          # imm = 0x10000
je 0x30f96
movl $0x1, 0x3deb72(%rip)    # 0x40fc74
imull $0xc8, %esi, %eax
movslq %eax, %rsi
movq %rcx, %rax
cqto
idivq %rsi
movl %eax, 0x3deb5f(%rip)    # 0x40fc78
jmp 0x30f96