// File: /workspace/github_c_makefile_star2_O1/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
cmpl $0xc8, 0x184728(%rip)   # 0x1b7a28
movsbl %dil, %ecx
jne 0x3333e
leaq 0x149dac(%rip), %rax    # 0x17d0b9
cmpb $0x0, (%rax)
jne 0x3333e
leaq 0x149d8f(%rip), %rax    # 0x17d0a8
cmpl %ecx, (%rax)
jne 0x3333e
cmpl %esi, 0x3d3cc9(%rip)    # 0x406fec
jne 0x3333e
cmpl %edx, 0x3d3cc5(%rip)    # 0x406ff0
jne 0x3333e
xorl %ebx, %ebx
leaq 0x149d6e(%rip), %rax    # 0x17d0a4
cmpl %ebp, (%rax)
je 0x33517
leaq 0x149d5f(%rip), %rax    # 0x17d0a4
movl (%rax), %r14d
movl %esi, %edi
movl %edx, %esi
movl %ebp, %edx
callq 0x6cb10
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x33517
movl $0x3, %eax
cmpl $0x8, %ebp
jg 0x33374
jne 0x3337d
xorl %eax, %eax
cmpl $0x8, %r14d
je 0x3337d
leaq 0x5c65d5(%rip), %rcx    # 0x5f9950
movl %eax, (%rcx)
leaq 0x6fcac(%rip), %rax     # 0xa3030
movslq (%rax), %rsi
movl %esi, 0x3d3c5f(%rip)    # 0x406fec
leaq 0x6fca0(%rip), %rax     # 0xa3034
movl (%rax), %ebp
movl %ebp, 0x3d3c54(%rip)    # 0x406ff0
xorl %eax, %eax
movl %eax, 0x3e28cc(%rip)    # 0x415c70
movl %eax, 0x3e28ca(%rip)    # 0x415c74
movl %esi, %eax
xorl $0x140, %eax            # imm = 0x140
movl %ebp, %ecx
xorl $0xc8, %ecx
orl %eax, %ecx
je 0x333d8
movslq %ebp, %rax
movl %esi, %ecx
xorl $0x280, %ecx            # imm = 0x280
movl %eax, %edx
xorl $0x190, %edx            # imm = 0x190
orl %ecx, %edx
jne 0x3351e
movl $0x10000, 0x3e2896(%rip) # imm = 0x10000
movq 0x378117(%rip), %rdi    # 0x3ab500
testq %rdi, %rdi
je 0x333fe
callq 0x61c0
movq $0x0, 0x378102(%rip) # 0x3ab500
movl %ebp, %eax
shll $0x5, %eax
movslq %eax, %rdi
callq 0x69a0
movq %rax, 0x3780ee(%rip)    # 0x3ab500
testq %rax, %rax
je 0x33509
shll $0x4, %ebp
movq %rax, 0x3e285b(%rip)    # 0x415c80
movslq %ebp, %rcx
addq %rax, %rcx
movq %rcx, 0x3e2856(%rip)    # 0x415c88
movl 0x3d3bb8(%rip), %eax    # 0x406ff0
addl %eax, %eax
movl %eax, 0x3e2850(%rip)    # 0x415c90
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x70805(%rip)     # 0xa3c50
movl %eax, 0x707fb(%rip)     # 0xa3c4c
movl %eax, 0x707f1(%rip)     # 0xa3c48
leaq 0x149e26(%rip), %rbx    # 0x17d284
movl (%rbx), %edi
callq 0x6dcbc
movl 0x3d3b85(%rip), %ecx    # 0x406ff0
testl %ecx, %ecx
js 0x33494
movl (%rbx), %eax
leaq 0x4(,%rcx,4), %rdx
xorl %esi, %esi
leaq 0x3e281e(%rip), %rdi    # 0x415ca0
xorl %r8d, %r8d
movl %esi, (%r8,%rdi)
addq $0x4, %r8
addl %eax, %esi
cmpq %r8, %rdx
jne 0x33485
movl 0x3d3b52(%rip), %edx    # 0x406fec
decl %edx
decl %ecx
xorl %edi, %edi
xorl %esi, %esi
callq 0x33586
leaq 0x5c64a2(%rip), %rax    # 0x5f9950
cmpl $0x3, (%rax)
jne 0x334bd
callq 0x4d9ef
callq 0x4db98
movl 0x184541(%rip), %edi    # 0x1b7a04
leaq 0x2480e6(%rip), %rsi    # 0x27b5b0
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x33747
xorl %edi, %edi
callq 0x339a1
cmpl $0x0, 0x7075f(%rip)     # 0xa3c40
jns 0x334fd
movl 0x3e497b(%rip), %eax    # 0x417e64
movl %eax, 0x70751(%rip)     # 0xa3c40
movl 0x24759f(%rip), %eax    # 0x27aa94
sarl %eax
movl %eax, 0x3e496b(%rip)    # 0x417e68
movl $0xc8, 0x184521(%rip)   # 0x1b7a28
jmp 0x33517
leaq 0x42e06(%rip), %rax     # 0x76316
movq %rax, 0x184509(%rip)    # 0x1b7a20
movl %ebx, %eax
popq %rbx
popq %r14
popq %rbp
retq
imulq $0xf0, %rsi, %rdi
shlq $0x16, %rax
leaq (%rax,%rax,4), %rcx
movq %rcx, %rax
cqto
idivq %rdi
movl $0x13333, 0x3e2739(%rip) # imm = 0x13333
cmpl $0xffff, %eax           # imm = 0xFFFF
jg 0x33555
movl $0x1, 0x3e2720(%rip)    # 0x415c70
jmp 0x333e2
cmpl $0x10000, %eax          # imm = 0x10000
je 0x333e2
movl $0x1, 0x3e270a(%rip)    # 0x415c74
imull $0xc8, %esi, %eax
movslq %eax, %rsi
movq %rcx, %rax
cqto
idivq %rsi
movl %eax, 0x3e26f7(%rip)    # 0x415c78
jmp 0x333e2