C++ ASCII game console screen flickering - c++

I am making a simple game using C++
It's just a tile game with an ASCII map.
The game itself works fine, but the console screen(map) is flickering when I move my player and I don't know how to fix this. Any help appreaciated, thanks!
Code:
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <vector>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
vector<string> map;
int playerX = 10;
int playerY = 10;
int oldPlayerX;
int oldPlayerY;
bool done = false;
void loadMap();
void printMap();
void setPosition(int y, int x);
void eventHandling();
int main()
{
loadMap();
map[playerY][playerX] = '#';
printMap();
while(!done){
eventHandling();
printMap();
}
exit(1);
return 0;
}
void eventHandling(){
char command;
command = _getch();
system("cls");
oldPlayerX = playerX;
oldPlayerY = playerY;
if(command == 'w'){
playerY--;
}else if(command == 'a'){
playerX--;
}else if(command == 'd'){
playerX++;
}else if(command == 's'){
playerY++;
}
if(map[playerY][playerX] == '#'){
playerX = oldPlayerX;
playerY = oldPlayerY;
}
setPosition(playerY,playerX);
}
void setPosition(int y, int x){
map[oldPlayerY][oldPlayerX] = '.';
map[y][x] = '#';
}
void printMap(){
for(int i = 0 ; i < map.size() ; i++){
cout << map[i] << endl;
}
}
void loadMap(){
ifstream file;
file.open("level.txt");
string line;
while(getline(file, line)){
map.push_back(line);
}
}

std::cout is not intended to be used that way.
You should refer to the system specific API for the target OS and environment. For example, for Windows you should use Console API functions for your purpose. These functions are defined in Wincon.h include file.

It also helps if you use a double buffering system such that only what needs to be overwritten every frame is changed. IO operations are extremely expensive so ought to be minimized.
Cameron Gives a Very Thorough Description of How to Do this Here
But in essence, you'd use two arrays, one containing the current state of the map, one containing the previous state and only write to the specific locations that have changed.

One method to clear the screen that works on many systems is to print the form feed character, \f. The Linux console supports this, and so did MS-DOS if you loaded ansi.sys. Unix has ncurses and terminfo to abstract these functions.

Related

Comparison of unsigned int is always true (npos issue?)

I’m receiving an error:
error: comparison of constant 18446744073709551614 with
expression of type 'unsigned int' is always true
[-Werror,-Wtautological-constant-out-of-range-compare]
The line of code it is referring to is this:
if (key_index != std::string::npos)
key_index is an unsigned int local variable in my main() function that my if statement is also in.
I read through a few posts on here already, trying to make sense of where I’m going wrong.
This link was the most informative
What does string::npos mean
But, even utilizing the advice from there, I’m still coming up with the same errors.
I’ve tried to create my own variable:
const int NPOS = -1
I’ve modified the comparison operator a few different ways.
I’ve tried turning the line into:
if(key_index != (std::string::npos -1))
I’ve tried a few other minor changes, which I cannot recall at the moment.
I forgot to write them down, and I apologize for not having all of my test/mod details.
This is the full main() code the issue is coming from. For all I know, there might be something else that I need to correct instead.
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <math.h> // For pow()
#include <string>
#include <stdexcept> // For std::runtime_error
#include <vector>
#include "GuitarString.hpp"
const sf::Uint32 quit_key = 27; // ASCII 27 is the Escape key
const std::string keyboard = "q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' ";
const int num_keys = keyboard.length();
const int kDuration = 8;
std::vector<sf::Int16> makeSamplesFromString(GuitarString *gs) {
std::vector<sf::Int16> samples;
gs->pluck();
for (int i= 0; i < kSampleRate * kDuration; i++) {
gs->tic();
samples.push_back(gs->sample());
}
return samples;
}
int main() {
sf::Event event;
double frequency;
unsigned int key_index;
std::vector < std::vector<sf::Int16> > samples(num_keys);
std::vector<sf::SoundBuffer> buffers(num_keys);
std::vector<sf::Sound> sounds(num_keys);
for (int i = 0; i < num_keys; ++i) {
frequency = 440 * pow(2, (i-24) / 12.0L);
GuitarString gs = GuitarString(frequency);
samples[i] = makeSamplesFromString(&gs);
if (!buffers[i].loadFromSamples(&samples[i][0],
samples[i].size(), 2, kSampleRate)) {
throw std::runtime_error("sf::SoundBuffer: failed to load from samples.");
}
sounds[i].setBuffer(buffers[i]);
}
sf::Vector2u size_win(500, 200);
sf::Sprite background_sprite;
sf::Texture background_texture;
if (background_texture.loadFromFile("keyboard.png")) {
background_sprite.setTexture(background_texture);
size_win = background_texture.getSize();
}
sf::RenderWindow window(sf::VideoMode(size_win.x, size_win.y),
" PS5B GuitarHero - Press Escape to Exit ");
window.setKeyRepeatEnabled(false);
while (window.isOpen()) {
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::TextEntered:
key_index = keyboard.find(event.text.unicode);
if (key_index != std::string::npos) { // the line causing the issue….
sounds[key_index].play();
} else if (event.text.unicode == quit_key) {
window.close();
}
break;
default:
break;
}
window.clear(sf::Color::Black);
window.draw(background_sprite);
window.display();
}
}
return 0;
}
If you're on a 64bit system, std::string::npos is almost certainly a 64bit number. To ensure maximum portability, use the type specified by std::string.
std::string::size_type key_index;
That should work, regardless of whether you're on a 32 or 64 bit system.

Playing video by long key press using open cv

I need to make small video player with OpenCV, which have to support the following functionality. Key 'p' on keyboard - pause/unpause, 'q' - exit, left and right arrow keys - play video frame by frame straight and reverse when it is paused. So the problem is when I try to show video with high quality and I hold arrow key for several seconds it does not run, but freeze and then jump to current frame after I release key. I tried to fix this with adding this_thread::sleep after cv::imshow() to give time to draw, but it did not help at all. So here is the code. Also, I have some reasons to use boost instead of C++11, so it is ok.
main.cpp
#include "VideoPlayer.hpp"
#include <iostream>
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "Video file full name required as argument." << std::endl;
}
VideoPlayer vp(argv[1]);
if (!vp.play())
return 1;
return 0;
}
VideoPlayer.hpp
#pragma once
#include <opencv/cxcore.hpp>
#include <opencv/highgui.h>
#include <string>
class VideoPlayer
{
public:
VideoPlayer(const std::string &video, const std::string &windowName = "Output window",
unsigned int delay = 30);
bool play();
private:
cv::VideoCapture videoCapture_;
std::string windowName_;
unsigned int delay_;
private:
bool processKey(int key);
void setFrame(int frameNum);
};
VideoPlayer.cpp
#include "VideoPlayer.hpp"
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
VideoPlayer::VideoPlayer(const std::string &video, const std::string &windowName,
unsigned int delay)
: videoCapture_(video)
, windowName_(windowName)
, delay_(delay)
{}
bool VideoPlayer::play()
{
if (!videoCapture_.isOpened()) {
std::cerr << "Unable to open video." << std::endl;
return false;
}
cv::namedWindow(windowName_);
for (;;) {
cv::Mat frame;
videoCapture_ >> frame;
cv::imshow(windowName_, frame);
int key = cv::waitKey(delay_);
if (processKey(key))
break;
}
return true;
}
bool VideoPlayer::processKey(int key)
{
if (key == 'p') {
for (;;) {
int pausedKey = cv::waitKey(0);
if (pausedKey == 'p') {
break;
} else if (pausedKey == 'q') {
return true;
} else if (pausedKey == 65363) {
int frameNum = videoCapture_.get(CV_CAP_PROP_POS_FRAMES);
setFrame(frameNum);
} else if (pausedKey == 65361) {
int frameNum = videoCapture_.get(CV_CAP_PROP_POS_FRAMES);
setFrame(frameNum - 2);
}
}
} else if (key == 'q') {
return true;
}
return false;
}
void VideoPlayer::setFrame(int frameNum)
{
if (frameNum > 0 && frameNum < videoCapture_.get(CV_CAP_PROP_FRAME_COUNT)) {
std::cerr << frameNum << std::endl;
videoCapture_.set(CV_CAP_PROP_POS_FRAMES, frameNum);
cv::Mat frame;
videoCapture_ >> frame;
cv::imshow(windowName_, frame);
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
}
}
I also created a multithreading implementation with buffer based on std::queue with lock, but it didn't solve the problem. And I tried to use boost::lockfree::queue, but I could not manage to finish it because of some strange behavior. I will share this code later, if it is necessary.
So, if somebody knows some good practice, how to avoid this problem, help me please.
EDIT:
Replacing boost::this_thread::sleep(boost::posix_time::milliseconds(10)); with cv::waitKey(0) is bad, because is makes me to do two short presses on arrow key to change one frame, and it does not help, because holding key skips it very fast. So the following code helped, but it is too strange and it is necessary to select times each for each video.
void VideoPlayer::setFrame(int frameNum)
{
if (frameNum > 0 && frameNum < videoCapture_.get(CV_CAP_PROP_FRAME_COUNT)) {
std::cerr << frameNum << std::endl;
videoCapture_.set(CV_CAP_PROP_POS_FRAMES, frameNum);
cv::Mat frame;
videoCapture_ >> frame;
cv::imshow(windowName_, frame);
int times = 7;
for (int i = 0; i < times; i++)
cv::waitKey(10);
}
}
Also, I'm unable to use Qt or something else, only C++03 with boost and OpenCV.
I think I need some trick to make cv::waitKey(time) wait fortime whether any key pressed or not.

getting keyboard input in c using conio.h

I am trying to learn how to use graphics.h and conio.h libraries.I am developing a graphic program which i need to move a rectangle after keyboard input.ex: if player press right , rectangle should move right side.Problem is i don't know how to get user input.I need to get user input inside a loop continuous.Here is my code.Any help is appreciated(keyword,function name etc)
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void drawrect(int left,int top,int right,int bot);
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
drawrect(5,400,40,450); // default start position
firsttime=1;//counter for if its first time in for loop
int currentl=5;
int currentt=400;
int currentr=40;
int currentb=450;
if(firsttime==1)
{
//get user input and drawrectangle with new inputs
//if player press right add 5 to currentl and current r and
//redraw the rectangle
}
getch();
closegraph();
}
void drawrect(int left,int top,int right,int bot)
{
rectangle(left,top,right,bot);
}
You can use getch() or _getch() to read codes of keys and react on it. But some things you should think over.
1) loop is needed to perform continuois action in your program.
2) keys such as "arrow left", "arrow up", etc. is given by getch() as two codes - the first -32 and the second depends on key.
Use the following programm to see the loop example and to find codes for keys:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main(void)
{
char ch;
printf("Press 'q' to exit prom program\n");
do{
ch = _getch();
printf("%c (%d)\n", ch, ch);
} while( ch != 'q');
}
Its solved this code works thanks for help
#include
#include
void drawrect(int left,int top,int right,int bot);
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int firsttime=1;//counter for if its first time in for loop
int currentl=5;
int currentt=400;
int currentr=40;
int currentb=450;
char ch;
settextstyle(0, HORIZ_DIR, 1);
outtextxy(20, 20, "To start press 'S'");
ch = getch();
cleardevice();
drawrect(5,400,40,450); // default start position
while(ch!='q')
{
ch = getch();
switch (ch)
{
case KEY_RIGHT:currentr=currentr+5;
currentl=currentl+5;
break;
case KEY_LEFT:currentr=currentr-5;
currentl=currentl-5;
break;
}
cleardevice();
drawrect(currentl,currentt,currentr,currentb);
}
getch();
closegraph();
}
void drawrect(int left,int top,int right,int bot)
{
rectangle(left,top,right,bot);
}

C++ SetConsoleTextAttribute changing char in a string

I have a C++ string which contains some characters. How can I change the char colour if I meet certain chars? Below is the sample code:
#include <iostream>
#include "windows.h"
using namespace std;
int main()
{
HANDLE h;
h = GetStdHandle(STD_OUTPUT_HANDLE);
string str = "my name is meow.";
for(int i=0; i<str.length(); i++)
{
if(str[i] == 'm')
{
//change the char 'm' to red color..
}
cout<<str[i];
}
return 0;
}
if(str[i] == 'm')
{
SetConsoleTextAttribute(h, FOREGROUND_RED);
cout<<str[i];
}
else
{
SetConsoleTextAttribute(h, 15);
cout<<str[i];
}
maybe this is what you wanna do?

Input from a text file to get a char C++

I am having trouble with getting input after a modifier in the text of a .txt file.
What I want to do is if the .txt file has the word "type:" then anything after that will be put into a char.
My code so far:
#include "stdafx.h"
#include <windows.h>
#include "VKH.h"
#include "Strmif.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
void GetDocumentandRead() {
string line;
ifstream myfile (line1);
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline (myfile,line);
char aline[100];
strcpy(aline, line.c_str());
printf(aline, "\n");
if (line.compare("mouseup") == 0){
MouseUp(10);
}
if (line.compare("mousedown") == 0){
MouseDown(10);
}
if (line.compare("mouseright") == 0){
MouseRight(10);
}
if (line.compare("mouseleft") == 0){
MouseLeft(10);
}
if (line.compare("mouseclick") == 0){
MouseClick();
}
if (line.compare("enter") == 0){
Enter();
}
if (line.compare("ctrltab") == 0){
CtrlTab();
}
if (line.compare("tab") == 0){
Tab();
}
if (line.compare("altf4") == 0){
AltF4(0);
}
if (line.compare("alttab") == 0){
AltTab();
}
if (line.compare("mousecenter") == 0){
MouseCenter();
}
if (line.compare(6,5,"type:") == 0){
//Don't know what to put here...
}
}
myfile.close();
}
else printf("\nUnable to open file\n\n");
}
So after the "type:" in a text file it would type that using a function I have called TypeStr();
void TypeStr(char *lpszString)
{
char cChar;
while((cChar=*lpszString++)) // loops through chars
{
short vk=VkKeyScan(cChar); // keycode of char
if((vk>>8)&1){keybd_event(VK_LSHIFT,0,0,0);} // hold shift if necessary
keybd_event((unsigned char)vk,0,0,0); // key in
keybd_event((unsigned char)vk,0,KEYEVENTF_KEYUP,0); // key out
if((vk>>8)&1){keybd_event(VK_LSHIFT,0,KEYEVENTF_KEYUP,0);} // release shift if necessary
}
}
Any help would be appreciated. Thanks!
First you should rewrite your TypeStr function so that it takes a const char *. Like this
void TypeStr(const char *lpszString)
{
...
}
no other changes needed.
Then you should call that function from your code like this
if (line.compare(6,5,"type:") == 0){
TypeStr(line.c_str() + 11);
}
The reason that you have to change your TypeStr function to const char* (apart from it being general good style) is that the c_str() method of std::string returns a const char* not a char*.