// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movb %dil, %al
movb %al, -0x5(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
cmpl $0xc8, 0x1a9117(%rip)   # 0x1f5588
jne 0x4c4c1
leaq 0x16e79f(%rip), %rax    # 0x1bac19
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x4c4c1
movsbl -0x5(%rbp), %eax
leaq 0x16e77b(%rip), %rcx    # 0x1bac08
cmpl (%rcx), %eax
jne 0x4c4c1
movl 0x3f68b5(%rip), %eax    # 0x442d4c
cmpl -0xc(%rbp), %eax
jne 0x4c4c1
movl 0x3f68ae(%rip), %eax    # 0x442d50
cmpl -0x10(%rbp), %eax
jne 0x4c4c1
leaq 0x16e756(%rip), %rax    # 0x1bac04
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x4c4c1
movl $0x0, -0x4(%rbp)
jmp 0x4c7b7
leaq 0x405508(%rip), %rdi    # 0x4519d0
leaq 0x66dda(%rip), %rsi     # 0xb32a9
callq 0x63e0
leaq 0x16e729(%rip), %rax    # 0x1bac04
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x14(%rbp), %edx
movsbl -0x5(%rbp), %ecx
callq 0xa60a0
cmpl $0x0, %eax
jge 0x4c503
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4c7b7
cmpl $0x8, -0x14(%rbp)
jle 0x4c518
leaq 0x5a1410(%rip), %rax    # 0x5ed920
movl $0x3, (%rax)
jmp 0x4c533
cmpl $0x8, -0x14(%rbp)
jne 0x4c531
cmpl $0x8, -0x20(%rbp)
je 0x4c531
leaq 0x5a13f5(%rip), %rax    # 0x5ed920
movl $0x0, (%rax)
jmp 0x4c533
leaq 0x93eba(%rip), %rax     # 0xe03f4
movl (%rax), %eax
movl %eax, 0x3f680a(%rip)    # 0x442d4c
leaq 0x93eaf(%rip), %rax     # 0xe03f8
movl (%rax), %eax
movl %eax, 0x3f67ff(%rip)    # 0x442d50
movl $0x0, 0x4054f5(%rip)    # 0x451a50
movl $0x0, 0x4054ef(%rip)    # 0x451a54
cmpl $0x140, 0x3f67dd(%rip)  # imm = 0x140
jne 0x4c57d
cmpl $0xc8, 0x3f67d5(%rip)   # 0x442d50
je 0x4c595
cmpl $0x280, 0x3f67c5(%rip)  # imm = 0x280
jne 0x4c5a4
cmpl $0x190, 0x3f67bd(%rip)  # imm = 0x190
jne 0x4c5a4
movl $0x10000, 0x4054b9(%rip) # imm = 0x10000
jmp 0x4c62c
imull $0x140, 0x3f67a2(%rip), %edi # imm = 0x140
imull $0xf0, 0x3f6794(%rip), %esi # 0x442d4c
callq 0x4c7c0
movl %eax, -0x24(%rbp)
movl $0x12c00, %edi          # imm = 0x12C00
movl $0xfa00, %esi           # imm = 0xFA00
callq 0x4c7c0
movl %eax, 0x405483(%rip)    # 0x451a58
cmpl $0x10000, -0x24(%rbp)   # imm = 0x10000
jge 0x4c5ea
movl $0x1, 0x405468(%rip)    # 0x451a50
jmp 0x4c62a
cmpl $0x10000, -0x24(%rbp)   # imm = 0x10000
jle 0x4c628
movl $0x1, 0x405457(%rip)    # 0x451a54
movslq 0x3f674c(%rip), %rax    # 0x442d50
imulq $0x140, %rax, %rax      # imm = 0x140
movl %eax, %edi
movslq 0x3f6738(%rip), %rax    # 0x442d4c
imulq $0xc8, %rax, %rax
movl %eax, %esi
callq 0x4c7c0
movl %eax, 0x405430(%rip)    # 0x451a58
jmp 0x4c62a
jmp 0x4c62c
movl 0x3f671e(%rip), %eax    # 0x442d50
shll $0x2, %eax
cltq
shlq $0x2, %rax
movl %eax, -0x1c(%rbp)
cmpq $0x0, 0x39ca1a(%rip)    # 0x3e9060
je 0x4c65f
movq 0x39ca11(%rip), %rdi    # 0x3e9060
callq 0x35f10
movq $0x0, 0x39ca01(%rip) # 0x3e9060
movl -0x1c(%rbp), %eax
shll %eax
movslq %eax, %rdi
callq 0x35ef0
movq %rax, 0x39c9ed(%rip)    # 0x3e9060
cmpq $0x0, %rax
jne 0x4c693
leaq 0x66ca9(%rip), %rax     # 0xb3329
movq %rax, 0x1a8ef9(%rip)    # 0x1f5580
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4c7b7
movq 0x39c9c6(%rip), %rax    # 0x3e9060
movq %rax, 0x4053bf(%rip)    # 0x451a60
movq 0x39c9b8(%rip), %rax    # 0x3e9060
movslq -0x1c(%rbp), %rcx
addq %rcx, %rax
movq %rax, 0x4053b2(%rip)    # 0x451a68
movl 0x3f6694(%rip), %eax    # 0x442d50
shll $0x2, %eax
sarl %eax
movl %eax, 0x4053a9(%rip)    # 0x451a70
movl $0xffffffff, 0x94a77(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x94a69(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x94a5b(%rip) # imm = 0xFFFFFFFF
leaq 0x16e6f8(%rip), %rax    # 0x1bade4
movl (%rax), %edi
callq 0xa7550
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x3f6646(%rip), %eax    # 0x442d50
jg 0x4c737
movl -0x1c(%rbp), %edx
movslq -0x18(%rbp), %rcx
leaq 0x405366(%rip), %rax    # 0x451a80
movl %edx, (%rax,%rcx,4)
leaq 0x16e6c0(%rip), %rax    # 0x1bade4
movl (%rax), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4c701
movl 0x3f660f(%rip), %edx    # 0x442d4c
subl $0x1, %edx
movl 0x3f660a(%rip), %ecx    # 0x442d50
subl $0x1, %ecx
xorl %esi, %esi
movl %esi, %edi
callq 0x4c7e0
leaq 0x5a11c7(%rip), %rax    # 0x5ed920
cmpl $0x3, (%rax)
jne 0x4c768
callq 0x72930
callq 0x72b70
movl 0x1a8df6(%rip), %edi    # 0x1f5564
leaq 0x26c99b(%rip), %rsi    # 0x2b9110
xorl %edx, %edx
callq 0x4c9c0
xorl %edi, %edi
callq 0x4ccc0
cmpl $0x0, 0x949a6(%rip)     # 0xe1130
jge 0x4c7a6
movl 0x406f12(%rip), %eax    # 0x4536a4
movl %eax, 0x94998(%rip)     # 0xe1130
movl 0x26be56(%rip), %eax    # 0x2b85f4
sarl %eax
movl %eax, 0x406f02(%rip)    # 0x4536a8
movl $0xc8, 0x1a8dd8(%rip)   # 0x1f5588
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq