home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

c++ help

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2872
Discussions: 200,967, Posts: 2,379,657, Members: 246,333
Old June 9th, 2003, 11:50 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Jun 2003
Posts: 2
Question
c++ help

well, i've got a program that i started writing at the beginning of the school year and slowly progressed until it was mostly functional. i've got a small problem that i would like fixed. the program still runs fine, but its pretty annoying.

i'm using dev c++ and my program is a small rpg with really crappy graphics. basically all you can do now is run around (your character is a 'x') and you kill stuff. there is also a small town that i just got working again.

my problem is the movement part. you can move the character with the arrow keys, but every time you do it prints some crazy character at the bottom. it really lags the game and makes it run pretty crappy. for some reason this game runs faster on low-end computers rather than mine, which is about 4 times faster than the ones at school.

anyways, heres the coding for the movement:


//////////////////////////KEY PRESS (ARROW KEYS) FUNCTION///////////////////////
void keypress()
{
ch2 = getche();
if(ch2 == 0xE0)
{
ch2 = getche();
switch (ch2)
{
case 0x48: //up
v = (v - 1);
cave[v+1][h] = ' ';
checkUp();
steps = steps+1;
if(steps >= (random*2))
{
run_r_s=1;
steps=0;
monster();
}
break;
case 0x4B: //left
h = (h - 1);
cave[v][h+1] = ' ';
checkLeft();
steps = steps+1;
if(steps >= (random*2))
{
run_r_e=1;
steps=0;
monster();
}
break;
case 0x4D: //right
h = (h + 1);
cave[v][h-1] = ' ';
checkRight();
steps = steps+1;
if(steps >= (random*2))
{
run_r_w=1;
steps=0;
monster();
}
break;
case 0x50: //down
v = (v + 1);
cave[v-1][h] = ' ';
checkDown();
steps = steps+1;
if(steps >= (random*2))
{
run_r_n=1;
monster();
}
break;
}
cout << endl;
}
}


getch() is pretty screwy and doesnt work too well (unless you really know how to use it, i suppose)

does anyone know of another way to do the same sort of thing, but that is less crappy? i can send anyone the code and/or the exe if you want. thanks for any help.


scott



oh yah, i also want to know if there is a way to print the screen once and then move my guy around without having to redraw the screen every time, that really slows it down too. thanks for your help!

ps, since you have to use a non-free email i had to use my friends. if you can please email anything to me at invisiblewallman@Hotmail.com that would be awesome. thanks again!
skizle is offline   Reply With Quote
Old June 10th, 2003, 11:27 AM     #2 (permalink)
Member
 
Join Date: Apr 2002
Location: Georgia
Posts: 137
Looks to me like you are using getche when you really should just be using getch

getche is same as getch, EXCEPT for getche also echos the character onto the screen. (Maybe that is what the "crazy characters" you were talking about were)

An alternative would be to learn a bit of x86 assembly language, learn how to write inline asm, and then use the INT 16H interface for communicating with the keyboard. I believe Dev-C++ uses the highly annoying "AT&T style" of x86 assembly language. The IBM style is intuitive. The AT&T style is not. Good luck (I haven't coded using the AT&T style before, personally)

BTW, INT 16H is a BIOS interrupt, so it should work on all x86 OSes unless the OS overwrites it. (Which, even in those cases, the OS' version of INT 16H usually ends up doing equivalent work or else calling the original at the end)

BTW, not sure if this will speed up your code any, but when you have something like
h = (h + 1);
you can instead write
h += 1;
for the same effect.

Also,
h = (h - 1);
can be
h -= 1;

steps = steps+1;
can be
steps += 1;

etc....

Generally, anything of the form
var = var mod num;
where "var" is your variable
"mod" is one of the following
+,-,*,/,%,<<,>>,^
and "num" is a number or other variable can be written in the form
var mod= num;

The way to print the screen once and then not have to slow it down anymore (in other words, then only printing the parts that changed, afterwards) is gonna be kinda tricky.

If this is a Windows GUI program, you will need to specify clipping regions for an InvalidateRect call, and then only InvalidateRect the area which you wish to be redrawn.

If this is a CLI application instead (Command Line Interface), then you might be able to find out which area of VRAM is being written to for screen display, and then directly write bytes there....but only to the places that need changes. Unless this runs as a DOS program through a prompt box, though, I'm not entirely sure that will work. (Oh, it'll work, but might not always work...might have some bad side-effects/etc....)

A simpler alternative which won't work quite as well, but which will work better than what you are doing would be to have some sort of a flag somewhere, set the flag ONLY when something changes/needs to be drawn, and then in your drawing function, at the top, check for the flag, and if it isn't set, exit the function, and otherwise draw. This might make your program so that if you move other windows on top of it, and then move those windows off, that "ghosting" will occur, however. Just be sure not to move windows on top of it, if you decide to implement this way.
__________________
Jüš† ä €öm¶ù†Ê® §ÇÌÈñŒ mÅjÒ®
Jüš† ä gü¥ is offline   Reply With Quote
Old June 19th, 2003, 04:40 AM     #3 (permalink)
Junior Member
 
Join Date: Jun 2003
Posts: 2
well...most of that didnt make much sense, but i suppose it helps a bit. im not really in the mood to try and learn an asm language, so i dont think thats gonna happen. but i'll just have to deal with this.

the idea you suggested about not always reprinting the screen is a good one, but wont work for me because the user is always moving the character around, so something is always printed over the screen. i dunno what to do. i figured it would be easy like in basic where you can have print over something (not below it like in c++)

anyways, i'll have to ask some people i know if they can help me.

thanks for tryin.


scott



also, if anyone knows anything about getch and the like, i would like to know why all the "case 0x48" thingies work for only the arrow keys. i've looked online to find the code for other keys, but none of them seem to work...i know that you have to change the "if(ch2 == 0xE0)" thing to somethign else, and i've tried that too, but nothign seems to work.

anyways, thanks for any help you can give.


oh yeah, another thing i'm stuck on: i've got a building that you can enter into in a header file, but i've got the variables for the money and stats and such in the main file. is there any way to transfer variables between files? i'm not quite sure if thats the right way to phrase it, but thats the best i can do. i think i've tried redeclaring them in the header file, but then it yells at me for doing that, but it also yells at me if i try and use the variable from the main file in the header....its all very strange. thanks!

scott

thats it for real this time!
skizle is offline   Reply With Quote
Old June 19th, 2003, 05:11 PM     #4 (permalink)
Ultimate Member
 
nukes's Avatar
 
Join Date: Oct 2002
Location: Scotland, UK
Posts: 3,221
Send a message via AIM to nukes Send a message via Yahoo to nukes
If you want a nicer way to do graphics and input, and sound when you get to it, look at libSDL it is cross platform as well. www.libsdl.org
I managed to pick up the input and graphics bits in a few days, you just use BMP files, or you can use others if you link against SDL_image as well.
It makes game programming SO much easier, and it is much better than DirectX IMO (mainly because it is cross-platform and is really easy to pick up)
__________________
_____
NuKeS
nukes is offline   Reply With Quote
Old June 21st, 2003, 09:41 AM     #5 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
Learn about escape sequences.

e.g. on Unix-style terminals, ESC[1;32m gives bright green. or ESC[22G moves to column 22.

By the way, does your code really look like that? Have you been taught about indenting?
strangerstill is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3020)
Forty-six years ago today (10)
The disrespect of Obama by Russian .. (46)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Internet Lost (5)
windows 7 problem (7)
windows vista security holes (15)
Point and Shoot Camera Suggestions. (6)
Is the PSU I received dead? (13)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Recent Discussions
Delete an OS (8)
help me pls laptop just stopped worki.. (0)
Open With ..... Win7 (3)
windows vista security holes (15)
Help getting around port 80 for camer.. (4)
Laptop with wireless problem. (12)
Internet Lost (5)
Skillsoft Network+ Study Software Que.. (9)
virus blocking exe. files (1)
Point and Shoot Camera Suggestions. (6)
CPU fan stops spinning randomly (11)
Nvidia GTX 260 problem (1)
Modern Warfare 2: Who Bought It? (65)
Is the PSU I received dead? (13)
Print spooler problem (16)
Kingston Bluetooth Dongle Driver (1)
Multiple Restarts Required at Boot (3)
webcam (0)
upgrade for hp a6101 (0)
tv not turn on-makes clicking sound (2)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)


All times are GMT -4. The time now is 03:48 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28