Why _kbhit() freeze the thread? - c++

I want to know why _kbhit() in loop doesnt allow to work thread, in a code below there must be an output to the screen "Test!", but it doesnt happen. Why?
#include <iostream>
#include <process.h>
#include <conio.h>
using namespace std;
void Thread(void *par)
{
while (true)
{
cout<<"Test!"<<endl;
};
}
int main(int argc, char* argv[])
{
_beginthread( Thread, 0, NULL);
while(!_kbhit());
return 0;
}

Related

SDL Window won't pop up

My window won't pop up.
Console appears, and without opening window just says that "Application ended"
Here is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <windows.h>
using namespace std;
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Event zdarzenie;
int frame = 0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
return 0;
okno = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, NULL);
ekran = SDL_GetWindowSurface(okno);
}
return 0; is your bug. After the return the program ends because main() ends. No lines in main are executed after the return. You certainly did not want to end before you called SDL_CreateWindow.
Can a function continue after a return statement?
Change the code to
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <windows.h>
using namespace std;
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Event zdarzenie;
int frame = 0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
okno = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, NULL);
ekran = SDL_GetWindowSurface(okno);
}
In c++ return 0; can be omitted from main.
Can I omit return from main in C?
Also I would expect this code to issue a warning (about unreachable code) on many compilers. If it did not warn you may need to turn up the warning level of your compiler. If it did warn you need to pay more attention to the warnings..
You may also want to remove the global variables and instead make your variables local to main. Global variables are usually considered a bad practice.
Are global variables bad?
Also SDL_Init() can fail. You may want to check its return value and quit on failure logging the error.
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
https://wiki.libsdl.org/SDL_Init

LNK1561 entry point must be defined - SDL

I got a LNK1561 entry point must be defined error i tried somethings my self as stsyem settings to console and it still doesn't work. Here is my code for every class the SDl.h is from the SDL.h donwload page.
main.cpp:
#include <iostream>
#include "MainGame.h"
int main(int argc, char** argv) {
std::cout << "Enter any ket to quit...";
int a;
std::cin >> a;
return 0;
}
MainGame.cpp:
#include "MainGame.h"
MainGame::MainGame()
{
_window = nullptr;
_screenHeight = 1028;
_screenWidth = 768;
}
MainGame::~MainGame()
{
}
void MainGame::run() {
InitSystems();
}
void MainGame::InitSystems() {
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 1028, 768, SDL_WINDOW_OPENGL);
}
MainGame.h:
#pragma once
#include <SDL/SDL.h>
class MainGame
{
public:
MainGame();
~MainGame();
void run();
void InitSystems();
private:
SDL_Window* _window;
int _screenWidth;
int _screenHeight;
};
Allt his code is to open an Windowed frame on your computer en open an console with the text Press Any ket to quit... If i remove the SDL.h include and the SDL code it all works if i put the include back and not the SDL code it gives the error again.
Have you tried using MainGame in your main function? Chances that the compiler assumes that it is never used and you do not get the #include <SDL/SDL.h> directive to work. Also, consider changing #include <SDL/SDL.h> to #include "SDL/SDL.h".
Also you might consider using SDL_SetMainReady for the cases that you are not using SDL_Main as the entry point.

QT/C++: Getting LASTINPUTINFO to work

Hi I am learning C++ through QT and I'm on the part where I'm trying to get LASTINPUTINFO to work. Below is the code I have made to see how it works but it seems to be only returning a single value and doesn't ever change whenever I make any inputs.
Care to explain what I'm doing wrong? And maybe provide a working example so I could get a grasp.
I'm trying to run it on Windows 10 Pro 64-bit.
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>
#include <unistd.h>
using namespace std;
test()
{
LASTINPUTINFO lastii;
lastii.cbSize = sizeof(LASTINPUTINFO);
return lastii.dwTime;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (true) {
qDebug() << test();
sleep(1);
}
return a.exec();
}
Example output here.
138899896
138899896
138899896
138899896
138899896
138899896
138899896
Fixed code for reference. Thanks to Anders.
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <Windows.h>
#include <unistd.h>
#include <iostream>
using namespace std;
test()
{
LASTINPUTINFO lastii;
lastii.cbSize = sizeof(LASTINPUTINFO);
GetLastInputInfo(&lastii);
return (GetTickCount() - lastii.dwTime) / 1000;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (true) {
cout<<test()<<"\n";
sleep(1);
}
return a.exec();
}
LASTINPUTINFO is not a class, it is a simple C struct. You actually have to call a function to fill it:
DWORD test() {
LASTINPUTINFO lastii;
lastii.cbSize = sizeof(LASTINPUTINFO);
GetLastInputInfo(&lastii);
return lastii.dwTime;
}

XNextEvent Doesn't works for some reason

I'm trying to catch keypress events using XLib. But for some reasons XNextEvent not working.
I'm not receiving any errors, but it looks like my program stuck on the line of "XNextEvent" call.
Here is my code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main()
{
XEvent event;
KeySym key;
char text[255];
Display *dis;
dis = XOpenDisplay(NULL);
while (1) {
XNextEvent(dis, &event);
if (event.type==KeyPress && XLookupString(&event.xkey,text,255,&key,0) == 1) {
if (text[0]=='q') {
XCloseDisplay(dis);
return 0;
}
printf("You pressed the %c key!\n", text[0]);
}
}
return 0;
}
This is not how X11 windowing system works.
Read this carefully. The key point is :
The source of the event is the viewable window that the pointer is in.
You do not create a window, therefore your program doesn't receive keyboard events. Even if you created window, it has to have focus :
The window used by the X server to report these events depends on the window's position in the window hierarchy and whether any intervening window prohibits the generation of these events.
Working example
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main()
{
XEvent event;
Display *dis;
Window root;
Bool owner_events = False;
unsigned int modifiers = ControlMask | LockMask;
dis = XOpenDisplay(NULL);
root = XDefaultRootWindow(dis);
unsigned int keycode = XKeysymToKeycode(dis, XK_P);
XSelectInput(dis,root, KeyPressMask);
XGrabKey(dis, keycode, modifiers, root, owner_events, GrabModeAsync, GrabModeAsync);
while (1) {
Bool QuiteCycle = False;
XNextEvent(dis, &event);
if (event.type == KeyPress) {
cout << "Hot key pressed!" << endl;
XUngrabKey(dis, keycode, modifiers, root);
QuiteCycle = True;
}
if (QuiteCycle) {
break;
}
}
XCloseDisplay(dis);
return 0;
}

Window refuses to render

I recently got started trying to use SFML. For some reason my simple program will not render the window. I've tried throwing everything into main to see if there was an error in my code that had to do with multiple files etc. but to no avail.
I'll launch my program and nothing will appear.
What's the problem?
//main.h
#ifndef MAIN_H
#define MAIN_H
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace sf;
class game
{
public:
void startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME);
void log(const string logging);
game()
{
QUIT = false;
pendingFile.open("Log.txt", ios::out);
pendingFile << "---Brain Bread Log---";
}
~game()
{
pendingFile.close();
}
private:
bool QUIT;
ofstream pendingFile;
};
#endif
//main.cpp
#include "main.h"
void game::log(const string logging)
{
pendingFile << logging;
}
void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
Window Game(VideoMode(SCREEN_W, SCREEN_H, 32), SCREEN_NAME);
while(QUIT == false)
{
Game.Display();
}
}
int main(int argc, char* argv[])
{
game gameObj;
gameObj.startLoop(800, 600, "Brain Bread");
return 0;
}
I tried your code and it behaves exactly as I expect it to - that is to say that an iconless window with a black body pops up and it doesn't respond to events. Is that what you're getting? If not, you might need to rebuild SFML.
You might want to try introducing event-handling so that your startLoop looks more like this:
void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
// Init stuff
while (Game.IsOpened())
{
sf::Event newEvent;
while (Game.GetEvent(newEvent))
{
// Process event
}
// Do graphics stuff
Game.Display();
}
}