Here is a small code and when we press "é" key or special characters with for instance french keyboards, we get in the console weird characters such as "├®".
#include <iostream>
#include "SDL.h"
using namespace std;
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindow("test", 100, 100, 1920, 1080, SDL_WINDOW_RESIZABLE);
bool opened = true;
SDL_Event event;
while(opened)
{
SDL_StartTextInput();
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
{
opened = 0;
break;
}
case SDL_TEXTINPUT:
{
cout << event.text.text << endl;
break;
}
}
}
return EXIT_SUCCESS;
}
PS: I am using SDL 2
Just have to use TTF_RenderUTF8_Blended for render (output in file was fine).
Related
I am experimenting with keyboard input in SDL and I have encountered a strange problem. Whenever I get input it only outputs the appropriate response sometimes (Clicking X only sometimes closes the program, pressing 1 only sometimes outputs "you pressed 1". Here is my main code:
#include <iostream>
#include <SDL.h>
#include "Screen.h"
#include "Input.h"
using namespace std;
int main(int argc, char *argv[]) {
Screen screen;
Input input;
if (screen.init() == false) {
cout << "Failure initializing SDL" << endl;
}
while (true) {
if (input.check_event() == "1") {
cout << "You pressed 1" << endl;
} else if (input.check_event() == "quit") {
break;
}
}
SDL_Quit();
return 0;
and here is my Input class:
#include <iostream>
#include <SDL.h>
#include "Input.h"
using namespace std;
string Input::check_event() {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return "quit";
}
else if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_1:
return "1";
}
}
}
return "null";
}
Any help would be appreciated!
From the documentation of SDL_PollEvent():
If event is not NULL, the next event is removed from the queue and
stored in the SDL_Event structure pointed to by event.
Analyzing your code:
if (input.check_event() == "1") {
This removes the event, whatever it is, from the queue.
} else if (input.check_event() == "quit") {
Say the return value of the 1st call to check_event() was "quit", then this call won't return "quit" again, because this information is now lost.
To fix that, call check_event() only once per loop iteration and store the result in a temporary variable. Then use only that variable in the conditions:
while (true) {
string event = input.check_event();
if (event == "1") {
cout << "You pressed 1" << endl;
} else if (event == "quit") {
break;
}
}
I have been trying for hours to get a test SDL program up and running, but no matter what I try, it instantly terminates upon launching.
My code:
#include <iostream>
#include <SDL.h>
#undef main
using namespace std;
int main(int argc, char *argv[]) {
const int WIDTH = 800, HEIGHT = 600, SDLWP = SDL_WINDOWPOS_UNDEFINED;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL not working." << endl;
}
cout << "SDL working properly." << endl;
SDL_Window *window = SDL_CreateWindow("Test", SDLWP, SDLWP, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL) {
SDL_Quit();
}
bool quit = false;
SDL_Event event;
while(!quit) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I have gotten the program to actually do something by having this code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
cout << "SDL working properly." << endl;
}
Also, about the #undef main part, I need that because otherwise, the program will think that I am calling SDL_main.
Here is the library search path if that helps:
C:\Users\ (my username)\Desktop\SDL2-2.0.4\i686-w64-mingw32\lib
And the libraries themselves, written in order from top to bottom:
mingw32
SDL2main
SDL2
No other library path will give me no compiler errors except:
C:\Users\ (my username)\Desktop\SDL2-2.0.4\lib\x86
So it seems that the root of problem is:
#include <SDL.h>
I want to change specific words color in windows command prompt, it work like this just fine:
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string setcolor(unsigned short color){
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
return "";
}
int main(int argc, char** argv)
{
setcolor(13);
cout << "Hello ";
setcolor(11);
cout << "World!" << endl;
setcolor(7);
system("PAUSE");
return 0;
}
but I want my function work like this
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string setcolor(unsigned short color){
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
return "";
}
int main(int argc, char** argv)
{
cout << setcolor(13) << "Hello " << setcolor(50) << "World!" << setcolor(7) << endl;
system("PAUSE");
return 0;
}
when I run it only setcolor(13) works and then color never changes till the end , what should I do to fix this problem
My comment may be wrong, it might be possible with an I/O manipulator (like std::setw and family):
struct setcolor
{
int color;
setcolor(int c) : color(c) {}
std::ostream& operator()(std::ostream& os)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
return os;
}
};
Use it like before:
std::cout << "Hello " << setcolor(50) << "world\n";
Note: I have no idea if this will work, as I have not tested it.
The problem you have now with your current code (as shown in the question) is that setcolor is a normal function which returns a string, and you just call the functions and print their return value (the empty string).
You need to put the output into a separate function:
void WriteInColor(unsigned short color, string outputString)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
cout << outputString;
}
You can then call
int main(int argc, char** argv)
{
WriteInColor(13, "Hello");
WriteInColor(50, "World");
WriteInColor(7, "\r\n");
}
Still not a one liner but cleaner than your first option :)
I cant seem to load a image onto a SDL window.
Code:
#include <iostream>
#include <SDL2/SDL.h>
#include <stdio.h>
using namespace std;
SDL_Window *window;
SDL_Surface *imageSurface;
SDL_Surface *image;
void createWindow() {
SDL_INIT_EVERYTHING;
if(SDL_INIT_EVERYTHING <0) {
cout << "SDL failed to initialize!" << endl;
}
window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,0);
if (window==NULL) {
cout << "Window could not be made" << endl;
}
imageSurface = SDL_GetWindowSurface(window);
}
void close() {
SDL_FreeSurface(image);
SDL_DestroyWindow(window);
SDL_Quit();
}
void loadmedia() {
image = SDL_LoadBMP("Test.bmp");
if (image==NULL) {
cout << "Image could not be loaded" << endl;
}
}
int main() {
createWindow();
loadmedia();
SDL_BlitSurface(image, NULL, imageSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(10000);
close();
}
You are missing the message loop.
Basically, your application is receiving messages to process operations such as drawing the window and all.
What you are missing is something like that:
while (!done) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
done = true;
}
}
You can find a sample here: http://www.gamedev.net/topic/376858-a-proper-sdl-message-loop/
I am trying to make a program that will be able to detect key-presses with SDL.
My current code is a modified version of somebody elses (trying to get it to work before making my own version).
#include "SDL/SDL.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
//Start SDL
if(0 != SDL_Init(SDL_INIT_EVERYTHING)) {
std::cout << "Well I'm screwed\n";
return EXIT_FAILURE;
}
SDL_Surface* display;
display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_Event event;
bool running = true;
std::cout << "Cake"; //Testing output (doesn't work)
while(running) {
std::cout << "Pie"; //Again, testing output and again doesn't work
if(SDL_PollEvent(&event)) { //I have tried this is a while statement
switch(event.type) {
case SDL_KEYDOWN:
std::cout << "Down\n"; // Have tried "<< std::endl" instead of "\n"
break;
case SDL_KEYUP:
std::cout << "Up\n";
break;
case SDL_QUIT:
running = false;
break;
default:
break;
}
}
}
//Quit SDL
SDL_Quit();
return 0;
}
This code is supposed to detect any key-down/up and output it, but it doesn't output anything.
My ultimate goal is to make it detect the konami code and then do something.
I constantly update the code above making it identical to the one I am using (except with added comments of what people have suggested).
Also if it helps: g++ -o myprogram.exe mysource.cpp -lmingw32 -lSDLmain -lSDL is the command I am using to compile. (If you didn't figure it out from the command, I am running windows (7).)
No errors occur when compiling
I am getting now output whatsoever, which leads me to believe that my probs has nothing to do with the key-checking; however there is a chance that is incorrect.
SDL needs a window to receive events.
Uncomment your SDL_SetVideoMode() call:
#include <SDL/SDL.h>
#include <iostream>
using namespace std;
int main( int argc, char* argv[] )
{
if( 0 != SDL_Init(SDL_INIT_EVERYTHING) )
{
std::cout << "Well I'm screwed\n";
return EXIT_FAILURE;
}
SDL_Surface* display;
display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_Event event;
bool running = true;
while(running)
{
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
std::cout << "Down" << endl;
break;
case SDL_KEYUP:
std::cout << "Up" << endl;
break;
case SDL_QUIT:
running = false;
break;
default:
break;
}
}
}
SDL_Quit();
return 0;
}
SDL By default redirects output to stdout.txt
You should query for all the SDL events in the loop, not just the first one. Try this one to check all the events:
while( SDL_PollEvent( &event ) ){
...
}
also you can try to update the screen in the loop with:
SDL_Flip( display );
hey there i think you should go to your "project properties" then "linker settings" and "Subsystem" then choose "Console (/SUBSYSTEM:CONSOLE)" Otherwise you can't see what you typed in cout and in visual studio you cant use
#include"SDL/SDL.h" you should type #include<SDL.h>