Skip to main content
GameDev.net gamedev.net
🔒 Locked

pdcurses conversion, basic output help!!

Started by midnightshade May 28, 2010 at 8:06 PM 1 replies 1k views
Original Post
midnightshade
midnightshade
Hello, I recently started making a rogue-like and was to the point where i had a map and character moving around obstacles, with doors and blockable terrian. All was good, but I was using a custom header for console commands, and it down right sucked when it came to text output (very cumbersome when printing strings). So I heard about the pdcurses library and built me a copy to win32 using gcc. I set it up and it works, but I can't get this code to work. I adapted the simplest part from my budding rogue-like to the pdcurses console lib and it won't draw my '@' character at all. This code should move the '@' around on the blank screen, but nope. It only prints the string and exits with ESC. (so i know the lib is working) What is wrong with my code for drawing the character and using the arrow keys for input? (the documentation said that is how the keys are defined, I was going straight to ascii codes which ran me into trouble latter).

#include <string>
#include <sstream>

#include <curses.h>

#define ESC 27



int main( void )
{

	initscr(); //initialize curses
	 //initscr();
    keypad(initscr(),1);
    curs_set(0);
    int nKey;
	int nPlayerX=40, nPlayerY=12;
    printw("Press ESC to exit");
    refresh();

    
    
	while((nKey = getch()) != ESC )
	{
		// Output phase
		mvaddch( nPlayerX, nPlayerY, '@' );




		// Processing phase
		switch( nKey )
		{
			// Move down
			case KEY_UP:
				nPlayerY++;
				break;

			// Move left
			case KEY_LEFT:
				nPlayerX--;
				break;

			// Move right
			case KEY_RIGHT:
				nPlayerX++;
				break;

			// Move up
			case KEY_DOWN:
				nPlayerY--;
				break;

			case ESC:
			endwin();
				return 0;
				break;
		}
	}

	return 0;
}



please can someone help me with this. I want to convert the rest of my startup game, but can't if I can't even draw a character to the screen or collect arrow key input.
signal_
signal_
Curses... old school. Why not use a library like libtcod? It is so awesome for developing rl's.

Anyway on to your question. I think you may have a problem with your game loop. In its basic format the game loop looks like:

while ( gameRunning ){     getInputFromPlayer();     updateGameLogic();     drawToBackBuffer();     flushBackBufferToScreen();    }


So your game loop's gameRunning condition is that the escape key is not pressed. If it's not pressed you keep running the game. You are gathering player input, you are updating the game logic, based on which key the player presses, and then you are drawing to the back buffer via mvaddch; however, you are not flushing the back buffer to screen output. So, despite me never using curses before, I suspect that if you add refresh(); at the end of your game loop you'd be okay. It is this function, I suspect, that will flush the back buffer.

So your final code would look like (see my add this comment):
#include <string>#include <sstream>#include <curses.h>#define ESC 27int main( void ){	initscr(); //initialize curses	 //initscr();    keypad(initscr(),1);    curs_set(0);    int nKey;	int nPlayerX=40, nPlayerY=12;    printw("Press ESC to exit");    refresh();        	while((nKey = getch()) != ESC )	{		// Output phase		mvaddch( nPlayerX, nPlayerY, '@' );		// Processing phase		switch( nKey )		{			// Move down			case KEY_UP:				nPlayerY++;				break;			// Move left			case KEY_LEFT:				nPlayerX--;				break;			// Move right			case KEY_RIGHT:				nPlayerX++;				break;			// Move up			case KEY_DOWN:				nPlayerY--;				break;			case ESC:			endwin();				return 0;				break;		}                refresh();  // ADD THIS!!!	}	return 0;}


Does this work?
midnightshade
midnightshade
No :(`

I don't know why either. This worked fine in my old version. Even putting a simple print statement in the while loop isn't working. (I tried changing up the while loop, no go.

Very frustrating as this game loop should work logically.

Whats this libtcod? You know any good documentation/tutorials on it?


hmmm, I googled it looks like something I want to try.


(still buggin me why my code doesn't work.)


Thanks for the reply.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.