Homestar Runner Wiki Forum

A companion to the Homestar Runner Wiki
It is currently Sun Sep 06, 2026 12:00 am

All times are UTC




Post new topic Reply to topic  [ 17 posts ] 
Author Message
 Post subject: Some o' that C++ help?
PostPosted: Wed Oct 17, 2007 4:00 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
Arrg, is there any way to grab a single character from keyboard input and then move on?

Pseudo-code:
Code:
char ch;
ch = GrabSingleCharacterWithoutHittingEnter();
//do something with ch


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 4:52 am 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
I think the function you want is _getch(): http://msdn2.microsoft.com/en-us/librar ... 80%29.aspx

You can use _getche() if you also want the character to be displayed after the key is pressed.

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 5:14 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
I'd hug you if I could. I started skipping over getch() stuff because I tried using getch() from conio.h and it worked just like cin.get() but _getch() is exactly what I want.

Another question: Does the underscore in front of a function mean anything? Like a function with _ is typically a certain type of function?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 5:19 am 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
My guess is it's for backward compatibility... VC++ has its own function called "getch", and the ISO C++ function wanted to define its own function called "getch" that worked differently, so rather than forcing VC++ to change how its own getch function works -- which would break backward compatibility -- they decided to give it an underscore and call it _getch.

That's only a guess, but most names you see with an underscore have it for a reason like that.

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 1:39 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
Is there any way to more efficiently clear my screen than system("cls")? Apparently clrscrn(); is included in conio.h but I always get "first declaration of function clrscrn()" or "first declaration of function _clrscrn()" errors when compiling.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 1:55 am 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
Unfortunately, there is no standard way to do it. It will vary from compiler to compiler and OS to OS. system("cls") won't work on Unix systems (such as Linux), for example. But it looks like it's the easiest and best choice on Windows.

If you need a portable application, the standard way to handle that would be to use a third-party library that already handles stuff like this. But if you're doing this for a class, that's probably outside its scope. (Then again, you might impress the teacher!)

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 2:18 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
Thanks, but everything I've tried has been pretty bad. I just tried a third party update of conio.h and clrscr() sucks just as much as system("cls");

I wish I was working with C++ in school. =P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 2:26 am 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
A third-party update of conio.h? I don't think it's a good idea to give your compiler "third-party updates" of standard header files. The only exception I can think of is using STLport with old versions of Visual C++, but it should have a decent version of STL after version 6.

Also, even if you do use third-party header files, be sure not to just overwrite the original files. The "right" way to do it -- although I think it's almost always wrongheaded in the first place -- is to put them in a different directory and make sure the compiler sees the header files in that directory before it reads its own header files.

MikeMcG wrote:
I wish I was working with C++ in school. =P


Well, if you're not doing this for a class, then, go ahead and use a third-party library. I'd recommend PDCurses, because it's very similar to the "curses" library that's standard on Unix systems, so you can kill two birds with one stone.

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 2:39 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
PDCurses looks pretty useful. I just hope I stuck all the files in the right places.

Are there any more efficient ways of outputting a 2D array? I'm using two for loops nested in each other. I think that, in the mean time, I could try to make the outputting faster.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 2:44 am 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
A nested for loop is generally the best way to do it. There's usually no way to make it more efficient than that. You could do something such as using a C string for each row of your data structure, so you'd only need to cout once for each row, but that's more trouble than it's worth. Especially since a display routine for a program like this is just not going to be too slow. Trust me, your for loops will be perfectly fast on a 20-year-old machine. :)

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 3:09 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
Oh man, PDCurses are exactly what I wanted. I'm making a roguelike and this library is going to make things easier. But I'm not going to have fun wading through the rest of the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 8:17 pm 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
I've been trying to fix this weird problem I get. The program starts up and it should print the map (it does) and wait, but it prints the map and then clears it and waits for input. I give it input (I just mash any button) and it does nothing with that input. After doing that it goes back to normal. And I also can't figure out why strings refuse to print. :S

Code:
#include <iostream>
#include <curses.h>
using namespace std;

//I'm cutting out stuff like constants declarations and prototypes

int main(void){
    int ch;
    string message;

    initscr(); //Start curses
    noecho(); //Don't talk back to me, boy
    keypad(stdscr, true); //Turn on stuff like F1 and arrow keys

    buildRoom(1, 1, 20, 10); //buildRoom(x, y, width, height);
    buildRoom(13, 7, 10, 7);
    buildRoom(23, 11, 6, 5);
    buildRoom(30, 1, 6, 6);

    room[10][10] = player; //Drop the player (an ampersand) at (10,10)

    while(ch != 'q'){
             printRoom();
             cout<<message;
             message = ""; //Reset the message given. [DOESN'T WORK]
             ch = getch();
             flushinp(); //Flush input stream to stop the sticky key problem
             if(ch == KEY_UP || ch == KEY_LEFT || ch == KEY_DOWN || ch == KEY_RIGHT) movePlayer(ch);
             else if(ch == 'v') message = "The floor appears to be made of dots."; //[DOESN'T WORK]
             erase();
    }
    //On exit, reprint the map and print out a message, then wait for a keystroke
    printRoom();
    cout<<"Have a pleasant day! [Hit Any Key to Escape]";
    ch = getch();
    return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 8:38 pm 
Offline
User avatar

Joined: Thu Jul 22, 2004 5:07 pm
Posts: 890
Location: Royse City, TX
Crappy answer here, but do you have access to a debugger? Also, Intellitype, I pine for you!

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 11:51 pm 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
My guess is you cannot use cout with curses, and instead must use a printing routine that it supplies. You could always write an ostream wrapper that behaves the same as cout does, though, if you know how to do that.

By the way, if I were writing a rougelike game, I would not do it in C++ unless my specific aim were to learn more about C++, or maybe if I were going to develop it into a more advanced form of game where C++ might be necessary.

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 6:18 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
Aww, grumblecakes. I always get a nasty crash when I try printw("%s", message);

Writing the roguelike is pretty much a way for me to get to know C++. If I was being practical, what would you write it in?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 6:21 am 
Offline
User avatar

Joined: Mon Jul 05, 2004 1:57 am
Posts: 2981
Location: Oklahoma City
Well, I would choose Python, myself, but there are certainly other languages that work just as well, such as Ruby.

- Kef

_________________
404 sig not found


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 6:26 am 
Offline
User avatar

Joined: Sun Apr 15, 2007 7:20 pm
Posts: 2321
Location: Strawberries. :[
I could definitely do a roguelike in Python, but I know the language (sort of) and it wouldn't be as challenging.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group