0

I have encountered a strange behaviour while attempting to write a roguelike. I've made a simple loop printing letters in a filled rectangle shape. With normal (stdscr) window, or newly initialised window from derwin() all works fine. Loop within stdscr/newly initialised window from derwin().

But the issue starts to appear after I return the window pointer from the Game class. Letters seem to be printed without any patttern, and the window looks like it is covered on some parts of it. Loop, when the pointer is returned.

I've tried debugging, but didn't succeed. The cursor is moving, loop is working, letters are printed, but sometimes they get stuck in the astral projection level, and they doesn't show up.

Here is the code: Game.cpp

Game::Game() : m_winMode(WinMode::GameWin) {
[...]
 initscr();
 wresize(stdscr, WIN_HGHT, WIN_WDTH);
 m_gameWin = derwin(stdscr, GAMEWIN_HGHT, GAMEWIN_WDTH, 0, 0);
[...]
}

WINDOW * Game::getWindow(Game::WinMode t_mode) const {
[...]
switch (t_mode) {
case Game::WinMode::GameWin:
    return m_gameWin;
    break;
[...]
}

pdcurses-test.cpp - this is the main file

#include "stdafx.h"
#include "Game.h"
#include "Map.h"

int main() {
 Game game;
 game.prepareScreen();

 WINDOW * test = game.getWindow(Game::WinMode::GameWin);
 wclear(test);
 for (int i = 0; i <= 48; i++) {
    for (int y = 0; y <= 120; y++) {
        mvwaddch(test, i, y, '%');
    }
 }
 wrefresh(test);

Here is the full code: github.com/gebirgestein/pdcurses-test/tree/test/pdcurses-test/pdcurses-test

Thanks in advance.

Community
  • 1
  • 1

1 Answers1

0

Calling subwin creates and returns a pointer to a new window with the given number of lines, nlines, and columns, ncols. The window is at position (begin_y, begin_x) on the screen. (This position is relative to the screen, and not to the window orig.) The window is made in the middle of the window orig, so that changes made to one window will affect both windows. The subwindow shares memory with the window orig. When using this routine, it is necessary to call touchwin or touchline on orig before calling wrefresh on the subwindow.

Calling derwin is the same as calling subwin, except that begin_y and begin_x are relative to the origin of the window orig rather than the screen. There is no difference between the subwindows and the derived windows.

From Here. Try calling touchwin(stdscr) before calling wrefresh(test).

Community
  • 1
  • 1
  • 1
    Thanks for the answer, I must've overlooked touchwin() in the documentation. Anyway, I figured out that I can just use a newwin() function. –  Mar 08 '17 at 15:31