Can't get Console and SDL to work together

Aug 2, 2010 at 8:47pm
Hey,
I'm trying to create a program that displays a map and allows the user to put coordinates on the map and enter what the coordinate is. I want the user part to be in a console window, but I want the map to be displayed using SDL.

I can't figure out how to get them to work together. Please help.

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
#define MAP_RANGE 2999

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <SDL/SDL.h>

using namespace std;

void ClearScreen()
{
    cout << string( 100, '\n' );
}

int main()
{
    int x = 3000, y = 3000;
    vector< vector<string> > mapCoords;
    mapCoords.resize(x);
    for (int i=0; i<mapCoords.size(); i++)
    mapCoords[i].resize(y);

    cout << "\n\n\n\nPRESS ENTER TO CONTINUE..." << endl;
    cin.get();
    ClearScreen();

    cout << "Type 'a' to add a location.\nType 'e' to erase a location.\nType 'd' to display the map." << endl;
    char choice;
    cin >> choice;
    if (choice == 'a') {
        int xChoice = 0;
        int yChoice = 0;
        cout << "Please enter the 'x' coordinate to add: ";
        cin >> xChoice;
        cout << "Please enter the 'y' coordinate to add: ";
        cin >> yChoice;
        cout << "Please enter the description of the point: ";
        string message;
        cin.ignore();
        getline(cin,message);
        mapCoords[xChoice][yChoice] = message;
        cout << xChoice << " " << yChoice << endl;
        cout << mapCoords[xChoice][yChoice];
    }
    if (choice == 'e') {

    }
    if (choice == 'd') {

    }

    return 0;
}

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

    return 0;
}


As you can see at the moment, the SDL doesn't even do anything. I just need it to accept the function before I can do anything.

Thanks,
Tyler Petresky
tyler.is.number.one@gmail.com
Aug 2, 2010 at 9:01pm
SDL overrides the program entry point with a macro called 'main'. Unfortunately I'm not sure how it is possible to get around it.
Aug 2, 2010 at 9:04pm
So the functions with the args and junk has to be the main() function? Is it possible to have 2 main()?
Aug 2, 2010 at 9:43pm
SDL uses a nasty trick to hijack main() so that the library initializaes properly on all platforms. (I think it was foolish to do it that way... but that's beside the point.)

IIRC, SDL does not interfere with console I/O, but your console does need to be the active window for it to work (and there isn't any window manager -- at least not that I am aware of -- that permits more than one toplevel window to be active at the same time).

What you will likely want to do is reroute keyboard input (from within SDL -- using the string input functions) to echo to your console window. This is trickier than it looks at first blush.


Having two windows kind of bucks the "fullscreen" kind of capability SDL gives users anyway. Why not just have an I/O sub-window in your game program? It is easier to do than have a separate console window -- and you won't need to play with OS-specific stuff to do it.

Hope this helps.
Aug 2, 2010 at 9:45pm
I think the idea that you had of the sub-window is the idea I like the best. It sounds the simplest and best. Only problem is.... I have NO idea how to do that. hahaha. So, I'm kinda just stuck. I hate when my projects become stagnant.
Aug 2, 2010 at 10:33pm
The main() that's defined in SDLmain doesn't do anything critical. That's what SDL_Init() is for. If it's bothering you, you can simply #undef main before your own main(). You'll probably have to unlink SDLmain, too.
It's odd, though. Unless this is a native Windows application, using std::cin even with SDLmain should be no problem.

FYI, SDLmain does a few things, but nothing you can't do without. Most notably, parse command line arguments (it will interpret "a spaced argument" as a single argument), which your runtime probably does, anyway, and redirect stdout and stderr to stdout.txt and stderr.txt respectively.
Last edited on Aug 2, 2010 at 10:37pm
Aug 2, 2010 at 10:35pm
You just have to divvy up your display into two parts -- those parts into which you draw the game and those parts where you draw text. There isn't really any difference between that and drawing a sprite normally. Just take care with your clipping regions.

Hope this helps.
Aug 2, 2010 at 10:43pm
@helios: I wrote this code on a Windows Computer. So, I guess it is native to Windows.

@Duoas: Well yeah, but wouldn't interacting with a REAL window, be different than interacting with a console window?

Thanks
Aug 2, 2010 at 10:47pm
By "native Windows application" I mean "a Windows application that's not a Windows console application". Check the subsystem on your linker options. It should be "console", not "Windows".
Aug 2, 2010 at 10:48pm
OH! yeah it is console. WAIT! shouldn't i change it to GUI application if im using SDL?
Aug 2, 2010 at 10:56pm
SDL doesn't care about the subsystem, but if you change it to GUI, you definitely will not get a console (well, not one you can just use with std::c*).
Aug 2, 2010 at 10:57pm
so. if i change it to GUI app. how would i do Duoas' idea? the one of the split box where the display is in top and the user interface in the bottom?
Aug 3, 2010 at 1:28am
Always have the SDL documentation ready:
http://www.libsdl.org/cgi/docwiki.cgi/

There are two things you will have to concern yourself with:

The clipping area:
http://www.libsdl.org/cgi/docwiki.cgi/SDL_SetClipRect

Text input:
http://www.libsdl.org/cgi/docwiki.cgi/SDL_StartTextInput
http://www.libsdl.org/cgi/docwiki.cgi/SDL_StopTextInput
http://www.libsdl.org/cgi/docwiki.cgi/SDL_TextInputEvent
For text input, you'll want to write yourself a function like the STL getline() that initiates "text input" mode, enters an event loop to read the input characters and append them to the result string until the user presses ENTER (making sure to update the display appropriately, which will be easiest if you write yourself yet another function to properly output text in your text area), and terminates "text input" mode.

Good luck!
Aug 3, 2010 at 1:43am
Thanks!! :D I appreciate this very very much!! :)
Aug 3, 2010 at 6:22am
If you are still subscribed to this thread: The thing on the clipping area doesn't tell me where to put it in the code and how to use it. :/
Aug 3, 2010 at 11:32am
You have to draw things to the display the same way as ever. All a clipping region does is prevent attempts to draw stuff from affecting stuff outside the clipping bounds.
Topic archived. No new replies allowed.