[C] Programming Challenge

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

[C] Programming Challenge

Post by thegamefreak0134 »

Yes, that's C, not C++. Unfortunately. Let me explain.

I've been tasked by an instructor to make a text based game. We've been limited to only Pure C and the standard C library, such that the game will easily compile on multiple platforms. For your typical text based game, this is fine. We want to do something more intresting. We want to basically create a "normal" game, using ASCII art for the graphics, which is perfectly acceptable.

I've got a good handle on using console commands to quickly output to the screen and (more importantly) clear the screen very quickly. However, the game runs rather... flickery... I have no way to actually sync my outputs to the refresh rate of the screen, which kinda sucks.

If there was a way to use a timer to force the refresh rate to lower than the screen refresh rate, I think it would work fine. (Right now it would induce epileptic seizures even in non-epileptic people.) Does anyone know of a way that this can be done? Thanks.

-thegamefreak
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

If you are really using standard C, you shouldn't even be able to clear the screen or even move the cursor. That stuff is not part of the standard C library. It can be done with ANSI escape sequences (which fails on windows).
You know your hexadecimal output routine is broken when it displays the character 'G'.
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

Post by thegamefreak0134 »

Hmm... Perhaps on older versions of windows. I actually went out and found the escape sequences (they were quite hard to find, mind) and they all worked fine. Clearing the screen very quickly works fine, but has the mentioned flicker. We found a better way however. It still flickers a little, but it's more tearing than anything. It's:

Code: Select all

printf("\033[%i;%iH",y,x);
Interesting, but it gets the job done.

My issue now is with keyboard input, which is driving me mad. I know there must be a way to grab a character and not have to press enter. However, for the purposes of the game, I think we can get it going the hard way, and then convince the teacher to let us do something fancy later. I got it solved. ^_^

-gamefreak
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

The console is not really suitable for rich multimedia, and so fancy extensions (colour, direct mouse/keyboard input and so on) would be pretty OS specific as far as I'm aware.

I wrote up how to handle console access and input on Windows, but if you need it to be cross-platform that's clearly not going to work.
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

Post by thegamefreak0134 »

Correct you are indeed. However, both windows and linux use the same emulated machine to actually draw the console, thus the escape sequences are the same. (These escape sequences actually came from a linux tutorial, but they work fine on linux.) Problem is, these sequences are very specific to the GCC compiler, and other compilers (MS, for example) output garbage instead.

Now, I've gotten too advanced for my own good. I want to read keyboard input so badly I'm willing to learn a bit of inline ASM to do it. x86 ASM, mind. This is difficult for me, and I have no idea what the h-e-double hockey sticks I'm doing. I'm hoping someone here can help me, it's a very simple thing to do.

I was following a tutorial on the net, and they had me go into the debug window in windows, create the following program, and run it:

Code: Select all

mov ah,10
int 16
This ran beautifully, and the debug window output the info on the registers and everything, so I know it was working fine. Now, I basically want to run this little snippet of assembly in a C program, and then shove the ax register off to a memory location that C can read out. I know this is possible, but I am obviously clobbering some registers somewhere, because when I get to the assembly snippet I have in my code, the program promptly crashes. This is what I have so far:

Code: Select all

#include <stdio.h>

int main(void) {

printf("strike a key:\n");

asm("pushl %eax\n\t"
	"movb $10, %ah\n\t"
	"int $16\n\t"
	"popl %eax\n\t");
		 
return 0;
}
Please, please, please help me fix this. (It exits with all sorts of error codes, the likes of which I suspect mean I screwed something up.) Ideally, this will let me emulate a Getkey like on a calculator, and my simple little program can go from there.

As for my project requirements, this is perfectly acceptable. It will compile in a standards compliant C compiler, and that's all that was required. ^_^

It just... doesn't work... <sob> Help...

-Nick
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
Goplat
New Member
Posts: 12
Joined: Mon 16 Jul, 2007 2:46 pm

Post by Goplat »

thegamefreak0134: DEBUG has the odd fact that numbers are always hexadecimal. In most assemblers you have to indicate that explicitly (in GCC, you use $0x10 instead of $10). That might not be your only problem though. What's your compilation target? You can't use interrupts directly from either Windows apps or the protected mode DOS environment provided by DJGPP, you can only do it in real mode DOS.

Besides, using inline assembly is really, really, really non-standard C. A given piece of inline asm will not just be specific to a processor, it will be specific to a processor/compiler combination, and to a platform as well if it has platform-specific code like interrupts.
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

Post by thegamefreak0134 »

I don't mind it being specific to a particular processor, I don't know of any computers these days that use non-z86 processors. (Mac testing is not required.) I will definitely try specifying hex in there, I had not thought of that. My target compiler (the one the teacher will be using) is gcc, so that should work fine. What I'm not entirely sure of is if the cygwin environment will allow me to do interrupts, since it does a fair job of emulating a linux environment...

It's complicated, and I've pretty much given that idea up for now. I'm sure I can find some other alternative, asm is waaay over my head on a PC. ^_^

-gamefreak
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
Post Reply