keyboard event not loading in cpp for sdl - c++

I am trying to make it my screen print hello when I click the a key... And switch images when I press the 2 key... however when I do, nothing happens, I'm not sure why... it does not give me an error for some weird reason... if you have any ideas please let me know in the comments below. I have listed the code there!
#include <SDL.h>
#include <iostream>
int main(int argc, char* argv[])
{
SDL_Window* window = nullptr;
SDL_Surface* WindowSurface = nullptr;
SDL_Surface* image1 = nullptr;
SDL_Surface* image2 = nullptr;
SDL_Surface* currentImage = nullptr;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("sdl Window",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480,
SDL_WINDOW_SHOWN);
WindowSurface = SDL_GetWindowSurface(window);
image1 = SDL_LoadBMP("Red_sheet_full_2.bmp");
image2 = SDL_LoadBMP("Red_sheet_fullA.bmp");
currentImage = image2;
bool isRunning = true;
SDL_Event ev;
while (isRunning)
{
while (SDL_PollEvent(&ev) != 0);
{
if (ev.type == SDL_QUIT)
isRunning = false;
else if (ev.type == SDL_KEYDOWN)
{
switch (ev.key.keysym.sym)
case SDLK_a:
printf("hello");
break;
switch (ev.key.keysym.sym)
case SDLK_2:
currentImage = image1;
break;
}
}
SDL_BlitSurface(currentImage, NULL, WindowSurface, NULL);
SDL_UpdateWindowSurface(window);
}
SDL_FreeSurface(image1);
SDL_FreeSurface(image2);
SDL_DestroyWindow(window);
currentImage = image1 = image2 = nullptr;
window = nullptr;
SDL_Quit();
return 0;
}

The problem is in the switch statement. The break is outside the switch so, if the first event you receive is not an SDLK_a, it will break out of the while loop before printing hello and exit.
Here's a simplified version of what's happening: https://godbolt.org/z/9eEcEYGhj
#include <iostream>
int main(int argc, char* argv[])
{
while (true)
{
char c{'p'};
switch (c)
case 'a':
std::cout << "a\n";
break; // exits without printing anything
switch (c)
case 'b':
std::cout << "b\n";
break;
std::cout << c;
}
}
You have different ways to fix it.
One would be to write the switch statement in a proper way: https://godbolt.org/z/99MohaTMx
#include <iostream>
int main(int argc, char* argv[])
{
while (true)
{
char c{'a'};
switch (c)
{
case 'a':
std::cout << "a\n"; // forever prints "a\n"
break;
case 'b':
std::cout << "b\n";
break;
default:
break;
}
}
}
Another one, just for your current implementation, would be to use an if-else if instead of a switch.

Related

Utilisation of SDL_KEYDOWN

My program generates an image. I want to close the window by clicking on any key or by clicking with my pointer on the window cross.
It works only by clicking on the cross (Use of SDL_QUIT) but not with SDL_KEYDOWN. I tried also with SDL_SPACE, SDL_KEYUP but the result is the same.
So what am I missing in my code ?
I am using xcode.
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *imageDeFond = NULL, *zozor = NULL;
SDL_Rect positionFond, positionZozor;
SDL_Event event;
int continuer = 1;
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
imageDeFond = SDL_LoadBMP("lac_en_montagne.bmp");
SDL_BlitSurface(imageDeFond, NULL, ecran, &positionFond);
SDL_Flip(ecran);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_KEYDOWN:
{
printf("use of SDL_KEYDOWN");
continuer=0;
break;
}
case SDL_QUIT:
{
printf("use of SDL_QUIT");
continuer = 0;
break;
}
}
}
SDL_FreeSurface(imageDeFond);
SDL_Quit();
return EXIT_SUCCESS;
}

What Causes SDL_Renderer to be invalid?

I'm trying to make a 2D game where the background is a hockey rink. The error handler said the Render-er didn't initialize thus making the texture not initialize. I know the BMP loads because I don't get an error for that. Don't worry about the camera stuff. The background is supposed to scroll. I just need the background to render. The code may not be the best, but corrections are always appreciated.
#include <SDL/SDL.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int fps = 60;
SDL_Window *window;
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
int x = 0, y = 0;
SDL_Surface *screen;
SDL_Surface *background = SDL_LoadBMP("hockeyrink.bmp");
if(background == NULL)
{
SDL_ShowSimpleMessageBox(0, "Background init error", SDL_GetError(), window);
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if(renderer == NULL)
{
SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,background);
if(texture == NULL)
{
SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
}
const int speed = 5;
SDL_Rect camera;
camera.x = 0;
camera.y = 0;
camera.w = 800;
camera.h = 600;
bool b[2] = {0,0};
Uint32 start;
window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);
if (window == NULL)
{
cout << ("could not create window: %s/n", SDL_GetError());
return 1;
}
bool running = true;
while (running)
{
start = SDL_GetTicks();
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
SDL_DestroyTexture(texture);
SDL_FreeSurface(background);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0]=1;
break;
case SDLK_LEFT:
b[1]=1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0]=0;
break;
case SDLK_LEFT:
b[1]=0;
break;
}
break;
}
}
if(b[0])
{
x+=speed;
camera.y+=speed;
if (camera.y > 3000-800)
{
camera.y=0;
}
}
else if(b[1])
{
x-=speed;
camera.y-=speed;
if (camera.y <= 0)
{
camera.y = 2000-800;
}
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
if(1000/fps>SDL_GetTicks()-start)
{
SDL_Delay(1000/fps-(SDL_GetTicks() - start));
}
}
return 0;
}
When you create renderer, the window pointer does not point to anything(i.e. it is uninitialized).
You must create the window before creating the renderer.
So put the lines:
window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);
if (window == NULL)
{
cout << ("could not create window: %s/n", SDL_GetError());
return 1;
}
before the line: SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

SDL, How can I make the background picture render?

I had a render problem, but I fixed that. The problem is, is that the picture won't actually show up. I know the picture gets loaded because I have an error handler if it isn't loaded. I know the code is messy, but I just need the picture to show up.
#include <SDL/SDL.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int fps = 60;
SDL_Window *window;
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);
if (window == NULL)
{
cout << ("could not create window: %s/n", SDL_GetError());
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
int x = 0, y = 0;
SDL_Surface *screen;
SDL_Surface *background = SDL_LoadBMP("hockeyrink.bmp");
if(background == NULL)
{
SDL_ShowSimpleMessageBox(0, "Background init error", SDL_GetError(), window);
}
if(renderer == NULL)
{
SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,background);
if(texture == NULL)
{
SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
}
const int speed = 5;
SDL_Rect camera;
camera.x = 0; //Don't worry about this camera, I need this after i get the background working.
camera.y = 0;
camera.w = 800;
camera.h = 600;
bool b[2] = {0,0};
Uint32 start;
bool running = true;
while (running)
{
start = SDL_GetTicks();
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
SDL_DestroyTexture(texture);
SDL_FreeSurface(background);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0]=1;
break;
case SDLK_LEFT:
b[1]=1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0]=0;
break;
case SDLK_LEFT:
b[1]=0;
break;
}
break;
}
}
if(b[0])
{
x+=speed;
camera.y+=speed;
if (camera.y > 3000-800)
{
camera.y=0;
}
}
else if(b[1])
{
x-=speed;
camera.y-=speed;
if (camera.y <= 0)
{
camera.y = 2000-800;
}
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
if(1000/fps>SDL_GetTicks()-start)
{
SDL_Delay(1000/fps-(SDL_GetTicks() - start));
}
}
return 0;
}
You have initialized b[1] to 0 and the texture is rendered only when b[1] is 1 as in your code.
You should either put the lines:
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
before the loop,
or press the left key of your keyboard(i.e. SDLK_LEFT)
to render and present the image on the screen.
It works in my system, so it should work in yours, too.

SDL2.0 moving Sprite with keyboard

I have been working on this game using SDL2.0 and C++ and have hit a snag. I have created the move function and am calling the right rectangle. And have called the move function in the main loop and it just doesn't move.
Here is the code.
//
// Window.cpp
// Galaxy Shooter
//
// Created by Samrith on 02/03/14.
// Copyright (c) 2014 Sam Shankar. All rights reserved.
//
#include "Window.h"
void Window::create() {
Resources r;
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Galaxy Shooter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screen = SDL_GetWindowSurface(window);
if(window == NULL)
std::cout << " Window cannot be created. Error: " << SDL_GetError();
else {
r.load();
SDL_UpdateWindowSurface(window);
}
}
void Window::destroy() {
Resources r;
SDL_DestroyWindow(window);
r.free();
SDL_Quit();
}
void Window::run() {
bool done = false;
Resources r;
create();
while(!done)
{
while(SDL_PollEvent(&event) != 0)
{
if(event.type == SDL_QUIT)
{
done = true;
destroy();
}
r.move(event); //This is the move function
SDL_UpdateWindowSurface(window);
}
r.projectile();
SDL_UpdateWindowSurface(window);
}
}
SDL_Window * Window::getWindow() {
return window;
}
SDL_Surface * Window::getSurface() {
return screen;
}
And here is the resources source file which contains the move function:
//
// Resources.cpp
// Galaxy Shooter
//
// Created by Samrith on 06/03/14.
// Copyright (c) 2014 Sam Shankar. All rights reserved.
//
#include "Resources.h"
bool b[4] = {0, 0, 0, 0};
void Resources::load() {
background();
rocket();
//r.create();
}
void Resources::free() {
Sound s;
SDL_FreeSurface(bgimage);
SDL_FreeSurface(rimage);
//SDL_FreeSurface(screen);
s.destroy();
}
//void Resources::run() {
// Window w;
// w.run();
//}
void Resources::background() {
Window w;
Sound s;
//Load screen
screen = SDL_GetWindowSurface(w.getWindow());
if(screen == NULL)
std::cout << "Surface (background) error: " << SDL_GetError();
//Load image
bgimage = IMG_Load("bg2.jpg");
if(bgimage == NULL)
std::cout << "Image (background) error: " << SDL_GetError();
//Load rectangle and set values
bgrect.x = 0;
bgrect.y = 0;
bgrect.h = w.screenH();
bgrect.w = w.screenW();
//Blit image to screen
SDL_BlitScaled(bgimage, NULL, screen, &bgrect);
s.init(1);
}
void Resources::rocket() {
Window w;
Sound s;
//Load screen
screen = SDL_GetWindowSurface(w.getWindow());
if(screen == NULL)
std::cout << "Surface (rocket) error: " << SDL_GetError();
//Load image
rimage = IMG_Load("rocket.png");
if(rimage == NULL)
std::cout << "Image (rocket) error: " << SDL_GetError();
//Load rectangle and set values
rrect.x = 15;
rrect.y = w.screenH()/2;
rrect.h = 75;
rrect.w = 75;
//Blit image to screen
SDL_BlitScaled(rimage, NULL, screen, &rrect);
//s.init(2);
}
void Resources::move(SDL_Event event) {
//bool done = false;
Window w;
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0] = 1; break;
case SDLK_DOWN:
b[1] = 1; break;
case SDLK_RIGHT:
b[2] = 1; break;
case SDLK_LEFT:
b[3] = 1; break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0] = 0; break;
case SDLK_DOWN:
b[1] = 0; break;
case SDLK_RIGHT:
b[2] = 0; break;
case SDLK_LEFT:
b[3] = 0; break;
}
break;
}
if(b[0]==true)
rrect.y += 10;
if(b[1]==true)
rrect.y -= 10;
if(b[2]==true)
rrect.x += 10;
if(b[3]==true)
rrect.x -= 10;
}
void Resources::projectile() {
Window w;
SDL_PumpEvents();
int x, y;
SDL_GetMouseState(&x, &y);
if(SDL_GetMouseState(&x, &y) & SDL_BUTTON(1))
{
rrect.x = x;
rrect.y = y;
SDL_BlitScaled(rimage, NULL, screen, &rrect);
for(int i=rrect.x; i<=w.screenW(); i++)
rrect.x+=2;
}
}
Also, I have Googled this problem and tried out various tutorials and docs. This is my last resort and not the first place I turned to for this problem. So please be kind. :)
by declaring Resource r in every function you create a new instance of the Resource class every time thus creating many different Resource objects.
The same goes for the Window w variable you declare in every function of the class Resource: each function call will create a new window.
I suggest you to read about Object Oriented Programming and variable scoping.
Also, C++ is not the most friendly language to start learning about Object Oriented Programming.
If you are starting out, I suggest you to switch to a simpler language such as Java or C#.
Good luck.

C++ game loop example

Can someone write up a source for a program that just has a "game loop", which just keeps looping until you press Esc, and the program shows a basic image. Heres the source I have right now but I have to use SDL_Delay(2000); to keep the program alive for 2 seconds, during which the program is frozen.
#include "SDL.h"
int main(int argc, char* args[]) {
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
hello = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
SDL_Delay(2000);
SDL_FreeSurface(hello);
SDL_Quit();
return 0;
}
I just want the program to be open until I press Esc. I know how the loop works, I just don't know if I implement inside the main() function, or outside of it. I've tried both, and both times it failed. If you could help me out that would be great :P
Here is a complete and working example. Instead of using a frame-time regulation you can also use SDL_WaitEvent.
#include <SDL/SDL.h>
#include <cstdlib>
#include <iostream>
using namespace std;
const Uint32 fps = 40;
const Uint32 minframetime = 1000 / fps;
int main (int argc, char *argv[])
{
if (SDL_Init (SDL_INIT_VIDEO) != 0)
{
cout << "Error initializing SDL: " << SDL_GetError () << endl;
return 1;
}
atexit (&SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode (640, 480, 32, SDL_DOUBLEBUF);
if (screen == NULL)
{
cout << "Error setting video mode: " << SDL_GetError () << endl;
return 1;
}
SDL_Surface *pic = SDL_LoadBMP ("hello.bmp");
if (pic == NULL)
{
cout << "Error loading image: " << SDL_GetError () << endl;
return 1;
}
bool running = true;
SDL_Event event;
Uint32 frametime;
while (running)
{
frametime = SDL_GetTicks ();
while (SDL_PollEvent (&event) != 0)
{
switch (event.type)
{
case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE)
running = false;
break;
}
}
if (SDL_GetTicks () - frametime < minframetime)
SDL_Delay (minframetime - (SDL_GetTicks () - frametime));
}
SDL_BlitSurface (pic, NULL, screen, NULL);
SDL_Flip (screen);
SDL_FreeSurface (pic);
SDL_Delay (2000);
return 0;
}
Tried with something like
SDL_Event e;
while( SDL_WaitEvent(&e) )
{
if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) break;
}
? You can find many tutorials and example out there; just a fast-search example.
Added note: WaitEvent "freezes" the program so you can't do anything .. you just wait; other waiting technics can be desired (as PollEvent, or WaitEvent again after the initializtion of a timer).
Since you're already using SDL, you could use the SDL_PollEvent function to run an event loop, checking to see if the key press event was ESC. Looks like this would be along the lines of mySDL_Event.key.keysym.sym == SDLK_ESCAPE.
#include <conio.h>
...
while (!kbhit())
{
hello = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
}
...