SDL app freezes when call my a member function

Hello I have an SDL app that works fine, it has a main game loop and i noticed that if i add another while loop in main, the window freezes. This is my code for the main.cpp where the window is made.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "SDL.h"
#include "SDL_opengl.h"
#include "player.h"
#include "Invest.h"
#include <iostream>

using namespace std;

bool run =true;

SDL_Event event1;


int main( int argc, char* args[] )
{

float botx1;
float boty1;
//OBJECTS
player p1;
Invest guy;


//SDL/ Window set=up/////////////
  SDL_Init(SDL_INIT_EVERYTHING);
  SDL_Surface* screen;
  screen = SDL_SetVideoMode(600,400,32, SDL_OPENGL | SDL_SWSURFACE );
glViewport(0,0,600,400);
glLoadIdentity();
//disable depth checking
glDisable(GL_DEPTH_TEST);
/////////////////////////////////////////////////

p1.set(5,5, 30, 30, 180, 230, 30, true, 0, false, false);
guy.menu();
while (run)
{

p1.move();
run=p1.testClose();

    //End of events


glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //Start rendering phase
glOrtho( 0, 600, 400, 0, -1, 1 );


glColor4ub(200,200,200,0);
p1.draw();

SDL_GL_SwapBuffers();
glPopMatrix(); //End rendering phase






}

  SDL_Quit();

  return 0;
}

Which works fine, but as soon as i add this line:
guy.menu();
I add this line right at the start of the main loop.
The window freezes as soon as i start it. guy is my obecjt and menu is a function that couts and cins in the console, and doesn't involve any SDL code.
my whole invest class doesnt use any SDL, just console. The 'guy' object is that of the invest class. So i want to have a consol where you choose all your options, then the SDL window which displays graphics for the console.
But it freezes, thankyou in advance.
Last edited on
Using cin is blocking. That means program flow will not continue until cin gets the input it is expecting (or it errors).

So if you are putting cin calls in your game loop, you will have to give new input every frame.
Hey thankyou very much your right.. but how excactly can i fix the problem, or give new input every frame?
Don't check cin every frame. Only check it when you want your program to completely stop and wait for user input.

EDIT: you probably shouldn't be using cin at all, really. I mean if you have a graphical window open, the user probably won't be looking at the console.
Last edited on
ohh okay, but what if do i do when i have all my code run as a console app. And that code is like an rpg, where u enter numbers to choose options and things, but if you want to battle someone, you go to the SDL window and move arrow keys and that.. ?
Why don't you just do everything from the SDL window?
Yeah i guess that would be better, i will jsut get the library that allows me to use text in SDL. Thankyou for your advice
Topic archived. No new replies allowed.