Adding content to window using ncurses

Hi all!
I'm makeing a card game with ncurses. I have all the card and player objects in order but i'm stuck trying to get the gui to work.

I'm using multiple windows for different players deck and pile. Now to the problem: I can get all the windows to show with borders in the console but i can't get any content printed in the windows and I rally can't see why!?

I have been reading the guide on
http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/windows.html
but I don't get any further on the problem right now.

I really need an extra pair of eyes on my code.

thanks for the help!

PS. the complete code incl. Makefile can be found at:
github.com/DanBrehmer/cardGame

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef GUI_H
#define GUI_H
#include <ncurses.h>
#include "Player.h"
#include "Deck.h"
#include "Pile.h"
#include "Cpu.h"

class Win
{
public:
  Win() = default;
  ~Win(){delete win;}
  WINDOW* win = nullptr;  // pointer to the window

  int height; // height och window
  int width;  // width of window
  int startY; // were on screen window will start (y)
  int startX; // were on screen window will start (x)
  int yCoord = startY + height / 2; // were in window content will start (y)
  int xCoord = startX + width  / 2; // were in window content will start (x)
};

class Gui
{
 public:
  Gui();
  ~Gui();

  void print_player(Player*);
  void print_deck(Deck*);
  void print_pile(Pile*);

 private:

  Win* humWin_; // holds WINDOW pointer and position info. See Win class.
  Win* cpuWin_;
  Win* deckWin_;
  Win* pileWin_;

  void create_new_win_(WINDOW*, int, int, int, int);
  void delete_win_(WINDOW*);
};

#endif 


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "Gui.h"
#include <string>
Gui::~Gui()
{
  delete humWin_;
  delete cpuWin_;
  delete deckWin_;
  delete pileWin_;
}
Gui::Gui() : humWin_(new Win), cpuWin_(new Win), deckWin_(new Win), pileWin_(new Win)
{
  initscr();
  
  cbreak();
  keypad(stdscr, TRUE);
  refresh();
 
  // setting the positions for the windows
  humWin_->height = 7;
  humWin_->width = 25;
  humWin_->startX = 20;
  humWin_->startY = 15;

  cpuWin_->height = 7;
  cpuWin_->width = 25;
  cpuWin_->startX = 20;
  cpuWin_->startY = 0;

  deckWin_->height = 5;
  deckWin_->width = 7;
  deckWin_->startX = 24;
  deckWin_->startY = 8;

  pileWin_->height = 5;
  pileWin_->width = 7;
  pileWin_->startX = 33;
  pileWin_->startY = 8; 

  // generating new windows at init so print func have someting to delete.
  // wrefresh() not nessesary

  create_new_win_(cpuWin_->win, cpuWin_->height, cpuWin_->width, cpuWin_->startY, cpuWin_->startX);
  create_new_win_(humWin_->win, humWin_->height, humWin_->width, humWin_->startY, humWin_->startX);
  create_new_win_(deckWin_->win, deckWin_->height, deckWin_->width, deckWin_->startY, deckWin_->startX);
  create_new_win_(pileWin_->win, pileWin_->height, pileWin_->width, pileWin_->startY, pileWin_->startX);
  
     
}

void Gui::create_new_win_(WINDOW* win, int h, int w, int y, int x)
{
  win = newwin(h, w, y, x);
  box(win, 0, 0); //just to see what's going on.
  wrefresh(win);
}

void Gui::print_player(Player* plr)
{

  if(dynamic_cast<Cpu*>(plr) != nullptr)
    {
      // delete the old window
      delete_win_(cpuWin_->win);

      // make a new empty window
      create_new_win_(cpuWin_->win, cpuWin_->height, cpuWin_->width, cpuWin_->startY, cpuWin_->startX);
      
      // add content to the new window 
      mvwprintw(cpuWin_->win, cpuWin_->yCoord, cpuWin_->xCoord, "%s", plr->hand_str().c_str());
     
      if(plr->open_empty())
	{
          // adding content
	  mvwprintw(cpuWin_->win, cpuWin_->yCoord + 3, cpuWin_->xCoord, "%s", plr->hidden_str().c_str()); 
	}
      else
	{
          // or this content
	  mvwprintw(cpuWin_->win, cpuWin_->yCoord + 2, cpuWin_->xCoord, "%s", plr->open_str().c_str());
	  mvwprintw(cpuWin_->win, cpuWin_->yCoord + 3, cpuWin_->xCoord, "%s", "XX XX XX");
	}

      // show window with content
      wrefresh(cpuWin_->win);
    }
  else
    {
      // delete the old window
      delete_win_(humWin_->win);

      // make a new empty window
      create_new_win_(humWin_->win, humWin_->height, humWin_->width, humWin_->startY, humWin_->startX);
      
      // add content to the new window 
      mvwprintw(humWin_->win, humWin_->yCoord, humWin_->xCoord, "%s", plr->hand_str().c_str());
     
      if(plr->open_empty())
	{
          // adding content
	  mvwprintw(humWin_->win, humWin_->yCoord + 3, humWin_->xCoord, "%s", plr->hidden_str().c_str()); 
	}
      else
	{
          // or this content
	  mvwprintw(humWin_->win, humWin_->yCoord + 2, humWin_->xCoord, "%s", plr->open_str().c_str());
	  mvwprintw(humWin_->win, humWin_->yCoord + 3, humWin_->xCoord, "%s", "XX XX XX");
	}

      // show window with content
      wrefresh(humWin_->win);
    }
}
void Gui::print_deck(Deck* dp)
{
  delete_win_(deckWin_->win);
  create_new_win_(deckWin_->win, deckWin_->height, deckWin_->width, deckWin_->startY, deckWin_->startX);
   
  mvwprintw(deckWin_->win, deckWin_->yCoord, deckWin_->xCoord, "%s", "D D");
  mvwprintw(deckWin_->win, deckWin_->yCoord + 1, deckWin_->xCoord, "%s", dp->get_size_str().c_str());
  mvwprintw(deckWin_->win, deckWin_->yCoord + 2, deckWin_->xCoord, "%s", "D D");
  wrefresh(deckWin_->win);
}
void Gui::print_pile(Pile* pp)
{
  delete_win_(pileWin_->win);
  create_new_win_(pileWin_->win, pileWin_->height, pileWin_->width, pileWin_->startY, pileWin_->startX);
   
  mvwprintw(pileWin_->win, pileWin_->yCoord, pileWin_->xCoord, "%s", "P P");
  mvwprintw(pileWin_->win, pileWin_->yCoord + 1, pileWin_->xCoord, "%s", pp->get_size_str().c_str());
  mvwprintw(pileWin_->win, pileWin_->yCoord + 2, pileWin_->xCoord, "%s", "P P");
  wrefresh(pileWin_->win);
}

void Gui::delete_win_(WINDOW* win)
{
  wborder(win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
  wrefresh(win);
  delwin(win);   
}


main.cpp
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
29
30
31
32
33
34
35
#include <ncurses.h>
#include "Player.h"
#include "Human.h"
#include "Cpu.h"
#include "Deck.h"
#include "Pile.h"
#include "Gui.h"

using namespace std;


int main()
{
  Deck* dp = new Deck;
  Pile* pp = new Pile;
  Player* hum = new Human(dp, pp, "Dan");
  Player* cpu = new Cpu(dp, pp);
  Gui* myGui = new Gui();
  
  myGui->print_player(cpu);
  myGui->print_player(hum);
  myGui->print_deck(dp);
  myGui->print_pile(pp);
 
  getch();
  endwin();
 
  delete dp;
  delete pp;
  delete hum;
  delete cpu;
  delete myGui;
  
  return 0;
}
Sorry to not talk about your issue, ¿but don't you find kind of awkward to do delete new T ?
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.
you must be talking about main.cpp.. I return dynamicly allocated memory whem im done with it.. In this case the program will end so it might not be nessesary but i have learnd that it is good practice.. this is just a test program a might add. I'm not a c++ wiss bu this is how they teach at the university :)
I really like different point of views, how would you do with the dynamic memory when leaving a skope with out getting memory leaks?
Why declare them as a dynamiclly allocated memory when you can do it as not

something like :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
  Deck dp;
  Pile pp;
  Player hum = Human(dp, pp, "Dan");
  Player cpu = Cpu(dp, pp);
  Gui myGui = Gui();
  
  myGui.print_player(&cpu);
  myGui.print_player(&hum);
  myGui.print_deck(&dp);
  myGui.print_pile(&pp);
 
  getch();
  endwin();
 
  return 0;
}


I didn't re check my code but you should get the idea

Memory leaks will be cleaned up by the OS at exit
Topic archived. No new replies allowed.