NCURSES skips menu option - c++

Picture below shows what I see when program starts:
That is correct, however when I press right arrow key selector skips middle menu option and renames last one:
Here's my code. To compile it run g++ <source_file> -lmenu -lncurses
#include <ncurses.h>
#include <menu.h>
#include <vector>
#include <string>
#include <cstdlib>
int main()
{
// # Initialize curses
initscr();
start_color();
cbreak();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
// # Setup menu
std::vector<std::string> menuChoices;
menuChoices.push_back(" < CREATE > ");
menuChoices.push_back(" < VERIFY > ");
menuChoices.push_back(" < EXIT > ");
ITEM **menuItems = (ITEM **)calloc(menuChoices.size() + 1, sizeof(ITEM *));
for (unsigned int i = 0; i < menuChoices.size(); i++)
menuItems[i] = new_item(menuChoices[i].c_str(), menuChoices[i].c_str());
menuItems[menuChoices.size()] = (ITEM *)NULL;
MENU *menu = new_menu((ITEM **)menuItems);
set_menu_mark(menu, NULL);
set_menu_format(menu, 1, menuChoices.size());
menu_opts_off(menu, O_SHOWDESC);
// # Setup window
const int windowHeight = 6;
const int windowWidth = 70;
WINDOW *window = newwin(windowHeight, windowWidth, (LINES - windowHeight) / 2, (COLS - windowWidth) / 2);
keypad(window, TRUE);
box(window, 0, 0);
set_menu_win(menu, window);
set_menu_sub(menu, derwin(window, 1, 38, windowHeight - 1, (windowWidth - 38) / 2));
// # Post the menu
post_menu(menu);
wrefresh(window);
int keyPressed;
while ((keyPressed = wgetch(window))) {
switch (keyPressed) {
case KEY_RIGHT:
menu_driver(menu, REQ_RIGHT_ITEM);
break;
case KEY_LEFT:
menu_driver(menu, REQ_LEFT_ITEM);
break;
}
wrefresh(window);
}
// # Unpost and free all the memory taken up
unpost_menu(menu);
for (unsigned int i = 0; i < menuChoices.size() + 1; i++)
free_item(menuItems[i]);
free_menu(menu);
endwin();
return 0;
}
How can I fix it?

The problem is that the program makes these calls in the wrong order:
set_menu_format(menu, 1, menuChoices.size());
menu_opts_off(menu, O_SHOWDESC);
The menu_opts_off call changes the width of menu items; the set_menu_format call needs to know the width of the menu items. Reversing the order of the calls makes the program work as intended.

Related

C++, ncurses: unable to display menu before a key is pressed

With a friend I am working on a tetris like game within a terminal. We are using ncurses to manage it and we have different "views". For example a view manages the grid while the other manages the Menu. However we have a problem: when we start the game, we need to press a key in order to see it, otherwise the window is empty.
Here is the code ran when we create the menu (the constructor):
MenuScreen::MenuScreen()
{
if (has_colors())
{
use_default_colors();
start_color();
init_pair(WHITEONRED, COLOR_WHITE, COLOR_RED);
init_pair(WHITEONBLUE, COLOR_WHITE, COLOR_BLUE);
init_pair(REDONWHITE, COLOR_RED, COLOR_WHITE);
}
char *choices[] = /* The menu choices */
{
(char *)" Solo ",
(char *)" Bot ",
(char *)" Tetrix ",
(char *)" Quitter ",
NULL};
/* Calculate nchoices */
for (n_choices = 0; choices[n_choices]; n_choices++)
;
/* alloction of an iteam array for the menu */
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for (ssChoice = 0; ssChoice < n_choices; ++ssChoice)
my_items[ssChoice] = new_item(choices[ssChoice], NULL);
my_items[n_choices] = (ITEM *)NULL;
/* menu structure creation */
my_menu = new_menu((ITEM **)my_items);
/* symbole on the left of selected iteam*/
set_menu_mark(my_menu, "> ");
/* Windows Border cration */
wBorder = newwin(8, 40, LINES / 2 - 8 / 2, COLS / 2 - 40 / 2);
registerWindow(wBorder);
wattrset(wBorder, COLOR_PAIR(WHITEONRED));
windowsFilling(wBorder);
box(wBorder, 0, 0);
windowsBorderTitle(wBorder, " option ");
wUI = derwin(wBorder, 8 - 2, 40 - 2, 2, 2);
registerWindow(wUI);
set_menu_sub(my_menu, wUI);
set_menu_fore(my_menu, COLOR_PAIR(REDONWHITE));
set_menu_back(my_menu, COLOR_PAIR(WHITEONRED));
/* menu display */
post_menu(my_menu);
}
And when the next key is pressed:
void MenuScreen::next()
{
menu_driver(my_menu, REQ_DOWN_ITEM);
update();
}
And here is what update() contains:
void MenuScreen::update()
{
touchwin(wUI);
wrefresh(wUI);
touchwin(wBorder);
wrefresh(wBorder);
}
If we remove update() from the next() function, the menu is no longer displayed when we press the next key, so we tried to add a call to update() at the end of the constructor, but this didn't work. We have no idea what could cause this bug. Here is our entire source code:
https://github.com/Th0rgal/poyuterm/tree/22e2422930a29fbe83414e382fd566c3eac69366
EDIT: Here is a minimal reproducible example made by my friend:
To build it, copy paste it in a menu.cpp file and type: g++ menu.cpp -o menu -lncurses -l menu
#include <stdlib.h> /* calloc() */
#include <string.h> /* strlen() */
#include <ncurses.h>
#include <menu.h>
#include <curses.h>
#define WHITEONRED 10
#define WHITEONBLUE 20
#define WHITEONBLACK 30
#define BLACKONWHITE 40
#define REDONWHITE 50
void windowsBorderTitle(WINDOW *pwin, const char *title)
{
int x, maxy, maxx, stringsize;
getmaxyx(pwin, maxy, maxx);
stringsize = 4 + strlen(title);
x = (maxx - stringsize) / 2;
mvwaddch(pwin, 0, x, ACS_RTEE);
waddch(pwin, ' ');
waddstr(pwin, title);
waddch(pwin, ' ');
waddch(pwin, ACS_LTEE);
}
void windowsFilling(WINDOW *pwin)
{
int y, x, maxy, maxx;
getmaxyx(pwin, maxy, maxx);
for (y = 0; y < maxy; y++)
for (x = 0; x < maxx; x++)
mvwaddch(pwin, y, x, ' ');
}
int main(int argc, char const *argv[])
{
initscr(); /* start ncurses */
cbreak(); /* immediately acquire each keystroke */
noecho(); /* do not echo user keystrokes */
keypad(stdscr, TRUE); /* enable detection of function keys */
int c;
ITEM **my_items;
MENU *my_menu;
WINDOW *wUI;
WINDOW *wBorder;
int n_choices;
int ssChoice;
int my_choice = -1;
if (has_colors())
{
use_default_colors();
start_color();
init_pair(WHITEONRED, COLOR_WHITE, COLOR_RED);
init_pair(WHITEONBLUE, COLOR_WHITE, COLOR_BLUE);
init_pair(REDONWHITE, COLOR_RED, COLOR_WHITE);
}
char *choices[] = /* The menu choices */
{
(char *)" Solo ",
(char *)" Bot ",
(char *)" Tetrix ",
(char *)" Quitter ",
NULL};
for(n_choices=0; choices[n_choices]; n_choices++);
/* ALLOCATE ITEM ARRAY AND INDIVIDUAL ITEMS */
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(ssChoice = 0; ssChoice < n_choices; ++ssChoice)
my_items[ssChoice] = new_item(choices[ssChoice], NULL);
my_items[n_choices] = (ITEM *)NULL;
/* CREATE THE MENU STRUCTURE */
my_menu = new_menu((ITEM **)my_items);
/* PUT > TO THE LEFT OF HIGHLIGHTED ITEM */
set_menu_mark(my_menu, "> ");
/* SET UP WINDOW FOR MENU'S BORDER */
wBorder = newwin(16, 40, 2, 20);
wattrset(wBorder, COLOR_PAIR(WHITEONRED) | WA_BOLD);
windowsFilling(wBorder);
box(wBorder, 0, 0);
windowsBorderTitle(wBorder, "Choose one");
/* SET UP WINDOW FOR THE MENU'S USER INTERFACE */
wUI = derwin(wBorder, 16-2, 40-2, 2, 2);
/* ASSOCIATE THESE WINDOWS WITH THE MENU */
set_menu_win(my_menu, wBorder);
set_menu_sub(my_menu, wUI);
/* MATCH MENU'S COLORS TO THAT OF ITS WINDOWS */
set_menu_fore(my_menu, COLOR_PAIR(REDONWHITE));
set_menu_back(my_menu, COLOR_PAIR(WHITEONRED) | WA_BOLD);
/* SET UP AN ENVIRONMENT CONDUCIVE TO MENUING */
keypad(wUI, TRUE); /* enable detection of function keys */
noecho(); /* user keystrokes don't echo */
curs_set(0); /* make cursor invisible */
/* DISPLAY THE MENU */
post_menu(my_menu);
touchwin(wBorder);
wrefresh(wBorder);
while(my_choice == -1)
{
touchwin(wBorder);
wrefresh(wBorder);
touchwin(wUI); /* refresh prior to getch() */
wrefresh(wUI); /* refresh prior to getch() */
c = getch();
switch(c)
{
case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
my_choice = item_index(current_item(my_menu));
pos_menu_cursor(my_menu);
break;
}
}
unpost_menu(my_menu);
for(ssChoice = 0; ssChoice < n_choices; ++ssChoice)
free_item(my_items[ssChoice]);
free_menu(my_menu);
delwin(wUI);
delwin(wBorder);
touchwin(stdscr);
wrefresh(stdscr);
endwin();
}

Ncurses wrefresh not showing window borders

I am experimenting with some C++ using ncurses and am having trouble showing the window borders, but am having trouble with the following programs.
#include <cstdio>
#include <cstdlib>
#include <ncurses.h>
int main(int argc, char **argv)
{
if(argc != 5)
{
printf("not enough arguments\n");
exit(1);
}
int height = atoi(argv[1]);
int width = atoi(argv[2]);
int y = atoi(argv[3]);
int x = atoi(argv[4]);
initscr();
WINDOW *win = newwin(height, width, y, x);
box(win, 0, 0);
wrefresh(win);
int py, px;
getparyx(win, py, px);
mvprintw(LINES-2, 0, "getparyx: (%d, %d)", py, px);
int by, bx;
getbegyx(win, by, bx);
mvprintw(LINES-1, 0, "getbegyx: (%d, %d)", by, bx);
getch();
delwin(win);
endwin();
}
In the program above I draw the border using box and refresh using wrefresh, but it doesn't show anything. The other stuff that I print to stdscr, however, does show.
In another program however I was able to get the border working.
#include <ncurses.h>
int main()
{
const int height = 6, width = 8;
WINDOW *win;
int starty, startx;
int ch;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
starty = (LINES - height) / 2;
startx = (COLS - width) / 2;
win = newwin(height, width, starty, startx);
box(win, 0, 0);
wrefresh(win);
while((ch = getch()) != KEY_F(1))
{
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(win);
delwin(win);
switch(ch)
{
case KEY_UP:
win = newwin(height, width, --starty, startx);
break;
case KEY_DOWN:
win = newwin(height, width, ++starty, startx);
break;
case KEY_LEFT:
win = newwin(height, width, starty, --startx);
break;
case KEY_RIGHT:
win = newwin(height, width, starty, ++startx);
break;
}
move(starty + (height / 2) - 1, startx + (width / 2) - 1);
box(win, 0, 0);
wrefresh(win);
}
delwin(win);
endwin();
}
The thing is that the border only appears in the loop. In other words the border does not start to show until I press buttons, meaning the initial wrefresh did not work.
After doing some research I this thread which suggested to call refresh after initscr (or at least before wrefresh()) but that did not work. So what am I missing that the border does not show in the first program?
For my tests, that initial refresh() is certainly what is missing. I checked some old code I wrote, and it indeed calls refresh() as part of the ncurses initialisation. Adding this to your code made it work for me. A lot of the curses documentation is still constrained to books, it never really made it onto the web.
initscr();
refresh(); // <-- HERE
WINDOW *win = newwin( height, width, y, x );
box( win, 0, 0 );
wrefresh(win);
I don't think the windowing model is fully initialised until after that first refresh() is called. But I could not find any documentation on exactly why that would be the case.
So not much detail in this answer, sorry... but I hope it helps.
The manual pages answer the question:
initscr
initscr also causes the first call to refresh(3x)
to clear the screen.
getch
If the window is not a pad, and it has been moved or modified since the
last call to wrefresh, wrefresh will be called before another character
is read.
The initscr (clear) and the mvprintw calls update stdscr, which is finally refreshed when you call getch. stdscr is a window, and as noted in the discussion of wrefresh, the physical screen is updated in the order that refreshes are applied (that is, it overlaps with the other window, and if you want the other window to appear, you should refresh stdscr first, to handle the clearing operation).

Text overlay not working with Xorg modesetting driver

This is a piece of code which displays text and background rectangle When this piece of code is run with Intel as default XORG driver everything works fine both text and rectangle are being displayed,whereas when i switch to the Modesetting driver only the background rectangle is seen and text is not being displayed
#include <iostream>
#include<unistd.h>
#include <sstream>
#include <string>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
int main()
{
Display *display = XOpenDisplay(NULL);
Screen *scn = DefaultScreenOfDisplay(display);
int screen_num = DefaultScreen(display);
int screen_width = DisplayWidth(display, screen_num);
int screen_height = DisplayHeight(display, screen_num);
int defaultScnDepth = DefaultDepthOfScreen(scn);
Visual *visual = DefaultVisualOfScreen(scn);
Window window=XCreateSimpleWindow(display,RootWindow(display,screen_num), 50, 50, 400, 400, 2 ,BlackPixel(display,screen_num),WhitePixel(display,screen_num));
//XFlush(display)
XMapWindow(display, window);
XShmSegmentInfo shmInfo;
XImage *xImage;
Pixmap backPixmap;
(xImage) = XShmCreateImage(display, visual, defaultScnDepth, ZPixmap, NULL, &shmInfo, screen_width, screen_height);
shmInfo.shmid = shmget(IPC_PRIVATE, (xImage)->bytes_per_line * (xImage)->height, IPC_CREAT | 0777);
shmInfo.shmaddr = (char *) shmat(shmInfo.shmid, 0, 0);
xImage->data = shmInfo.shmaddr;
shmInfo.readOnly = False;
XShmAttach(display, &shmInfo);
(backPixmap) = XShmCreatePixmap(display, window, (char *) (shmInfo.shmaddr), &shmInfo, (xImage)->width, (xImage)->height, (xImage)->depth);
XGCValues values;
GC gc = XCreateGC(display, backPixmap, 0, &values);
XSync(display, false);
Drawable drawable =backPixmap;
visual = DefaultVisual(display, DefaultScreen(display));
Colormap colormap = XCreateColormap(display, window, visual, AllocNone);
//gc = XCreateGC(display, drawable, 0, &values);
//XFlushGC(display, gc);
const char *text = "Hello";
XftDraw *xftDraw = NULL;
XRenderColor xrFGColor, xrBGColor;
XftColor xftFGColor, xftBGColor;
XftFont *font = NULL;
font = XftFontOpenName( display, DefaultScreen( display ), "morpheus-18" );
xftDraw = XftDrawCreate(display, drawable, visual, colormap);
int nextLineStartY, rectYRef;
bool firstIte;
unsigned int rectX, rectY, rectWidth, rectHeight;
nextLineStartY = 0; rectYRef = 0;
firstIte = true;
rectX = 0; rectY = 0; rectWidth = 0; rectHeight = 0;
std::istringstream strStream(text);
std::string line;
while(std::getline(strStream, line))
{
const char *lineText = line.c_str();
if(*lineText == '\0')
{
nextLineStartY += rectHeight + 1;
continue;
}
const char *text = lineText;
XGlyphInfo extents;
XftTextExtents8(display, font, (XftChar8 *)text, strlen(text), &extents);
unsigned int width = extents.width;
unsigned int height = extents.height;
int ascent = extents.y;
int lBearing = extents.x;
rectX = 50 - lBearing - 1;
rectY = 50 - ascent - 1;
rectWidth = width + 2 * 1;
rectHeight = height + 5 + 1;
if(firstIte)
{
rectYRef = rectY;
firstIte = false;
}
int diff = rectYRef - rectY;
rectY += nextLineStartY + diff;
nextLineStartY += rectHeight + 1;
if(1)
{
xrBGColor.red = 0x7fff;
xrBGColor.green= 0x7fff;
xrBGColor.blue = 0x7fff;
xrBGColor.alpha= 0xffff;
XftColorAllocValue(display, visual, colormap, &xrBGColor, &xftBGColor);
// Draw background fill rectangle
XftDrawRect(xftDraw, &xftBGColor, rectX, rectY, rectWidth, rectHeight);
XftColorFree(display, visual, colormap, &xftBGColor);
}
xrFGColor.red = 0xbfff;
xrFGColor.green = 0xbfff;
xrFGColor.blue = 0xbfff;
xrFGColor.alpha= 0xffff;
XftColorAllocValue(display, visual, colormap, &xrFGColor, &xftFGColor);
// Overlay Text
XftDrawString8(xftDraw, &xftFGColor, font, 50, 50, (XftChar8 *) text, strlen(text));
XColor xForeColor;
xForeColor.red = 0xafff;
xForeColor.green = 0xafff;
xForeColor.blue = 0xffff;
if(XAllocColor(display,colormap,&xForeColor))
XSetForeground(display,gc,xForeColor.pixel);
XftColorFree(display, visual, colormap, &xftFGColor);
XFreeColors(display, colormap, &(xForeColor.pixel), 1, 0);
}
XftDrawDestroy(xftDraw);
XShmPutImage(display, window, gc, xImage, 0, 0, 0, 0, 400, 400, false);
XSync(display, false);
getchar();
}
I tried out other drivers too, with the radeon drivers i see a X error that shared pixmaps are not supported while i don't see any such error for the modesetting driver.
Has this something to do with the shared pixmaps, if yes how should i make it work with the modesetting driver.
I have been stuck on this for a while now, any help would be appreciated.

C++ SFML Collison Detection

I am trying to make a game where you gave to dodge the incoming box (thing). I don't know how to code a quick and easy collision detection using if statements.
I have tried to code this at "//collision detection". I want it so that if the arrow (player) is touching the box the game closes.
Main Menu.h
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#pragma once
class main_menu
{
public:
void startmenu(RenderWindow &gameDisplay, Font &font1, bool &MainMenu, RectangleShape &start, Event &evnt)
{
gameDisplay.setMouseCursorVisible(true);
Text starttext;
starttext.setCharacterSize(100);
starttext.setFont(font1);
starttext.setString("DODGE");
starttext.setFillColor(Color(255, 255, 255));
starttext.setStyle(Text::Bold);
FloatRect starttextRect = starttext.getLocalBounds();
starttext.setOrigin(starttextRect.width / 2, starttextRect.height / 2);
starttext.setPosition(Vector2f(1366 / 2.0f - 30, 768 - 700.0f));
RectangleShape outlinetop(Vector2f(1366, 50));
outlinetop.setFillColor(Color(255, 255, 255));
outlinetop.setPosition(0, 0);
RectangleShape outlinebottom(Vector2f(1366, 50));
outlinebottom.setFillColor(Color(255, 255, 255));
outlinebottom.setPosition(0, 718);
RectangleShape outlineright(Vector2f(50, 768));
outlineright.setFillColor(Color(255, 255, 255));
outlineright.setPosition(1316, 0);
RectangleShape outlineleft(Vector2f(50, 768));
outlineleft.setFillColor(Color(255, 255, 255));
outlineleft.setPosition(0, 0);
Text startbuttontext;
startbuttontext.setFont(font1);
startbuttontext.setCharacterSize(70);
startbuttontext.setString("PLAY");
startbuttontext.setFillColor(Color(255, 255, 255));
FloatRect startbuttontextRect = starttext.getLocalBounds();
startbuttontext.setOrigin(starttextRect.width / 2, startbuttontextRect.height / 2);
startbuttontext.setPosition(Vector2f(1366 / 2.0f + 60 - 30, 768 - 480.0f));
if (MainMenu == true)
{
Vector2f tempMouse(Mouse::getPosition(gameDisplay));
if (start.getGlobalBounds().contains(tempMouse) &&
evnt.type == Event::MouseButtonPressed &&
evnt.key.code == Mouse::Left)
{
MainMenu = false;
}
//mouseposx = (Mouse::getPosition(gameDisplay).x);
//mouseposy = (Mouse::getPosition(gameDisplay).y);
/*
while (Mouse::getPosition(gameDisplay).x > 1366 / 2.0 && Mouse::getPosition(gameDisplay).x < 1083)
while (Mouse::getPosition(gameDisplay).y > 768 - 450 && Mouse::getPosition(gameDisplay).y < 868)
if (Mouse::isButtonPressed(Mouse::Left))
MainMenu = false;
*/
if (Keyboard::isKeyPressed(Keyboard::Q))
{
MainMenu = false;
gameDisplay.close();
}
gameDisplay.draw(outlinebottom);
gameDisplay.draw(outlineleft);
gameDisplay.draw(outlineright);
gameDisplay.draw(outlinetop);
gameDisplay.draw(start);
gameDisplay.draw(starttext);
gameDisplay.draw(startbuttontext);
gameDisplay.display();
}
}
};
things.h
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
using namespace sf;
using namespace std;
#pragma once
int RandomXpos = 0;
class Thing
{
public:
void thingspawn(RenderWindow &gameDisplay, int &enemystartx, int &enemystarty, int &enemywidth, int &enemyheight, RectangleShape &thing, int enemyspeed)
{
thing.setFillColor(Color(255,255,255));
if (thing.getPosition().y < 800)
thing.move(0.0f, 0.4f);
if (thing.getPosition().y >= 800)
{
RandomXpos = rand() % 1000;
thing.setPosition(RandomXpos, -200);
}
}
};
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "things.h"
#include "Main Menu.h"
#include <cstdlib>
#include <Windows.h>
using namespace std;
using namespace sf;
int main()
{
//FreeConsole();
RenderWindow gameDisplay(VideoMode(1366, 768), "Game", Style::Fullscreen);
gameDisplay.clear();
int enemystarty = -200;
int enemystartx = 300;
int enemyheight = 30;
int enemywidth = 300;
int enemyspeed = 0.3;
int enemycount = 1;
int dodged = 0;
int blockcolor = 0;
bool MainMenu = true;
Font font1;
if (!font1.loadFromFile("OCRAEXT.ttf"))
{
//error...
}
RectangleShape start(Vector2f(400, 100));
start.setFillColor(Color(0, 0, 0));
start.setOutlineThickness(20);
start.setOutlineColor(Color(255, 255, 255));
FloatRect startRect = start.getLocalBounds();
start.setOrigin(startRect.width / 2, startRect.height / 2);
start.setPosition(Vector2f(1366 / 2.0f, 768 - 450.0f));
/*
RectangleShape player(Vector2f(200, 50));
player.setFillColor(Color(0, 0, 255));
player.setPosition(400, 700);
*/
Texture playertexture;
if (!playertexture.loadFromFile("whiter_arrow.jpg"))
{
//error...
}
Sprite player;
player.setTexture(playertexture);
player.setPosition(300, 600);
RectangleShape thing(Vector2f(enemywidth, enemyheight));
thing.setPosition(300, -200);
Event evnt;
while (gameDisplay.isOpen())
{
while (gameDisplay.pollEvent(evnt))
{
switch (evnt.type)
{
case Event::Closed:
gameDisplay.close();
case Event::KeyPressed:
if (Keyboard::isKeyPressed(Keyboard::Q))
gameDisplay.close();
}
}
main_menu object;
object.startmenu(gameDisplay, font1, MainMenu, start, evnt);
while (MainMenu == false)
{
gameDisplay.setMouseCursorVisible(false);
if (Keyboard::isKeyPressed(Keyboard::D))
if (player.getPosition().x < 1200)
{
player.move(0.3f, 0.0f);
}
if (Keyboard::isKeyPressed(Keyboard::A))
if (player.getPosition().x > 50)
{
player.move(-0.3f, 0.0f);
}
if (Keyboard::isKeyPressed(Keyboard::Q))
gameDisplay.close();
Thing thingobject;
thingobject.thingspawn(gameDisplay, enemystartx, enemystarty, enemywidth, enemyheight, thing, enemyspeed);
//collision detection
if (player.getPosition().y <= thing.getPosition().y)
if (player.getPosition().x == thing.getPosition().x)
gameDisplay.close();
gameDisplay.clear();
gameDisplay.draw(player);
gameDisplay.draw(thing);
gameDisplay.display();
}
}
}
There are many ways to do 2D collision detection between sets of objects ("do any of these objects touch each other") and that's beyond the scope of this question. What you seem to be asking is much simpler: "do two 2D rectangles touch?". Let's assume you have rectangles r1 and r2, then you can use:
collision = r1.x <= r2.x + r2.width &&
r2.x <= r1.x + r1.width &&
r1.y <= r2.y + r2.height &&
r2.y <= r1.y + r1.height;
It is also worth noting that in games, you must allow for the possibility of the move increment being too large because of object velocity vs frame rate, allowing objects to appear to move through each other. You can compensate by building bounding rectangles that encompass the before/after positions of both objects and testing that first, before diving into more complex calculations involving the intersection of the swept-through-time volumes.
There is SFML way to do the collision, it will work for your case.
you can do it like this:
if ( player.getGlobalBounds().intersects(thing.getGlobalBounds()) )
{
// player and thing is collided! do something
}
#Wheezil's answer is the standard way to do it, for axis-aligned object like your case. it's better to implement it yourself so you know how it happens :)

When i compile acode i have this error cannot open file X11/Xlib.h in c++

/* Sequential Mandelbrot program mandelbrot.c */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define X_RESN 800 /* x resolution */
#define Y_RESN 800 /* y resolution */
typedef struct complextype
{ float real, imag;
} Compl;
int main (int argc, char *argv[])
{ Window win; /* window initialization */
unsigned
int width, height, /* window size */
x, y, /* window position */
border_width, /* border width in pixels */
display_width,
display_height, /* size of screen */
screen; /* which screen */
char *window_name = "Mandelbrot Set", *display_name = NULL;
GC gc;
unsigned long valuemask = 0;
XGCValues values;
Display *display;
XSizeHints size_hints;
Pixmap bitmap;
XPoint points[800];
FILE *fp, *fopen ();
char str[100];
XSetWindowAttributes attr[1];
/* Mandlebrot variables */
int i, j, k;
Compl z, c;
float lengthsq, temp;
/* connect to Xserver */
if ( (display = XOpenDisplay (display_name)) == NULL )
{ fprintf (stderr, "drawon: cannot connect to X server %s\n",
XDisplayName (display_name) );
exit (-1);
}
/* get screen size */
screen = DefaultScreen (display);
display_width = DisplayWidth (display, screen);
display_height = DisplayHeight (display, screen);
/* set window size */
width = X_RESN;
height = Y_RESN;
/* set window position */
x = 0;
y = 0;
/* create opaque window */
border_width = 4;
win = XCreateSimpleWindow(display, RootWindow (display, screen),
x, y, width, height, border_width,
BlackPixel (display, screen), WhitePixel (display, screen));
size_hints.flags = USPosition|USSize;
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
size_hints.min_width = 300;
size_hints.min_height = 300;
XSetNormalHints (display, win, &size_hints);
XStoreName(display, win, window_name);
/* create graphics context */
gc = XCreateGC (display, win, valuemask, &values);
XSetBackground (display, gc, WhitePixel (display, screen));
XSetForeground (display, gc, BlackPixel (display, screen));
XSetLineAttributes (display,gc,1,LineSolid,CapRound,JoinRound);
attr[0].backing_store = Always;
attr[0].backing_planes = 1;
attr[0].backing_pixel = BlackPixel(display, screen);
XChangeWindowAttributes(display, win,
CWBackingStore | CWBackingPlanes | CWBackingPixel, attr);
XMapWindow (display, win);
XSync(display, 0);
/* Calculate and draw points */
for(i=0; i < X_RESN; i++)
{ for(j=0; j < Y_RESN; j++)
{ z.real = z.imag = 0.0; /* 800x800 scale factors */
c.real = ((float) j - 400.0)/200.0;
c.imag = ((float) i - 400.0)/200.0;
k = 0;
do
{ /* iterate for pixel color */
temp = z.real*z.real - z.imag*z.imag + c.real;
z.imag = 2.0*z.real*z.imag + c.imag;
z.real = temp;
lengthsq = z.real*z.real+z.imag*z.imag;
k++;
} while (lengthsq < 4.0 && k < 100);
if (k == 100) XDrawPoint (display, win, gc, j, i);
} }
XFlush (display);
sleep (30);
/* Program Finished */
}
Open up a command prompt and run the two commands:
find / -type f - name Xlib.h
find / -type d - name X11
That should help you locate that file and/or directory so you can see whether it's available.
If it is available (e.g., it's found at /usr/include/X_stuff/X11/Xlib.h), make sure your compiler command references it, like:
g++ -I/usr/include/X_stuff ...
If it's not there, install it.
Either you forgot to install the X development packages, or the directory containing the headers was not added to the includedir list.