Some (common?) code errors...

This is a discussion on Some (common?) code errors... within the C++ Programming forums, part of the General Programming Boards category; Hey! Sorry to bother you again This time, I'm attempting to make a really small game engine. It'll be using ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Question Some (common?) code errors...

    Hey!

    Sorry to bother you again
    This time, I'm attempting to make a really small game engine. It'll be using SDL for those who really wanna look into the code...
    Anyway, first, here's my code:

    Code:
    /*
    This is the main file to link to when developing with the Stupid Game Engine.
    NOTE:	This 'Game Engine' is not meant for any hardcore game development. 
    It is meant to be a simple library to link to in the event that you may want to
    create a *simple* game or rendering of something using the OpenGL 3d Library.
    
    Author:	FlyingIsFun1217
    Website:	None, this is too insignificant...
    Date:		5-7-07
    */
    
    // First, the headers needed to use OpenGL and SDL... DUH!...
    #include	<GL/gl.h>
    #include	<GL/glx.h>
    #include	"SDL.h"
    #include	<cstdlib>
    
    // Second, the headers needed for things *besides* OpenGL...
    #include <X11/X.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    
    void fatalError(char *message)
    {
    	fprint(stderr, "SGE: &#37;s\n", message);
    	exit(1);
    }
    
    const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
    if (!videoInfo)
    {
    	fatalError("Could not collect video information.");
    }
    
    class SGE
    {
    	public:
    		SGE();
    		~SGE();
    		void InitializeVideo(char ScreenMode, int SreenSizeX, int ScreenSizeY, int bitsPerPixel);
    		void UninitializeVideo();
    	private:
    		char SM;
    		int SSX;
    		int SSY;
    		int BPP;
    };
    
    SGE::SGE()
    {
    
    }
    
    SGE::~SGE()
    {
    
    }
    
    void SGE::InitializeVideo(char ScreenMode, int ScreenSizeX, int ScreenSizeY, int bitsPerPixel)
    {
    	// Collect values from user for creation of the window...
    	SM = ScreenMode;
    	SSX = ScreenSizeX;
    	SSY = ScreenSizeY;
    	BPP = bitsPerPixel;
    
    	// Create a pointer that references to the backbuffer...
    	SDL_Surface	*ScreenSurface;
    
    	// Initialize SDL with parameters given by user...
    	SDL_Init (SDL_INIT_VIDEO);
     	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
       		 fatalError("Could not initialize SDL Video.");
      	exit(1);
    
    	atexit(SDL_Quit);
    	
    	if (SM == windowed)
    	{
    		ScreenSurface = SDL_SetVideoMode (SSY, SSX, BPP);
    	}
    
    	else if (SM == fullscreen)
    	{
    		SDL_SetVideoMode (SSX, SSY, BPP, SDL_FULLSCREEN);
    	}
    }
    
    void SGE::UninitializeVideo()
    {
    	SDL_Quit();
    	exit(0);
    }
    And here go the questions on the errors...

    1. First error: 'fprint was not declared in this scope'. WHAT?!?! Isn't including iostream all I need??? (Line 25)

    2. Second error: 'expected unqualified-id before 'if'' on the if (!videoInfo). Not quite sure what that means, actually...

    3. Third error: 'windowed' and 'fullscreen' are undeclared. I understand this one, and have even found that you can use getchar() to return the value, but how would I put it in the if, else if statements like I had set up?

    4. Line 81, the '}' after the if statement with windowed, all it says is 'error: at this point in file'

    5. Line 92, exit(0) at end in UninitializeVideo function, it tells that 'a function-definition is not allowed here before '{' token. Huh?

    6. Line 94, it says that it 'expected: '}' at end of input'. Didn't I put one in? I see one!

    Sorry if you think that I am ranting, raving, trying to cheat by getting others' help, whatever. I just want to see how I would theoretically make an extremely simple game engine, and will be my basis for learning SDL, a little OpenGL, different functions like occlusion culling, etc.

    Thanks for all of your help!
    FlyingIsFun1217
    Last edited by Salem; 05-12-2007 at 11:23 PM. Reason: Folding long lines

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    1. Did you mean fprintf? If you want to write to stderr just use std::cerr.

    2. That code needs to be inside a function.

    Fix those first and then compile again. It is generally better to compile often as you go so you don't get a bunch of errors all at once.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    1. Yes, I was looking at it and just realized that. How stupid
    2. Ok, I'll recheck that

    Thanks again!
    FlyingIsFun1217

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Ok, now that I fixed both of those (I just removed the error message), I still need to figure out how to find out the way to problem with the if, else if statements dealing with the comparison between the string that should match either 'fullscreen' or windowed.

    Is there a way to use the getchar() function within the if, else if comparison check?

    Thanks again!
    FlyingIsFun1217

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Can you give more information on what you are trying to do? You mentioned a string, are you asking the user to enter "fullscreen" or "widescreen"? If so, you need to add code to get a word from the user. Usually cin is best for that in C++. Then, you need to compare the string you read in with those two strings. The correct way is to put them in double quotes. That indicates that it is a string and not a variable name or something.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by Daved View Post
    Can you give more information on what you are trying to do? You mentioned a string, are you asking the user to enter "fullscreen" or "widescreen"? If so, you need to add code to get a word from the user. Usually cin is best for that in C++. Then, you need to compare the string you read in with those two strings. The correct way is to put them in double quotes. That indicates that it is a string and not a variable name or something.
    Well, what I wanted to do was collect it as an argument in the InitializeVideo function (It should collect a char ScreenMode, which is turned into SM, which is what I want to check that is values 'fullscreen' or 'windowed'.

    Basically, if the user passed the function InitializeVideo like the following:

    Code:
    SGE->InitializeVideo(windowed, 300, 300, 16);
    It would use the if statement that checks to see that SM is 'windowed'. If it is like:

    Code:
    SGE->InitializeVideo(fullscreen, 300, 300, 16);
    It would use the if statement that checks to see that SM is 'fullscreen'.

    Thanks again for your help!
    FlyingIsFun1217

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    First, a char is a single character, so if you wanted to pass a string you'd want the string class (#include <string>).

    However, you don't really need a string here. You just have a variable that has two options, windowed or fullscreen. One solution is to make an enum type and pass the enum. If you know how to do that I'd say that's the way to go.

    Another solution is to just make that parameter a bool. If it is true, use windowed, if false use fullscreen. This has the disadvantage of not being scalable. You can't add another option, since bool is only true or false. It is also a little less clear.

    A third solution is to use a char, but pass in a char. For example, pass in 'w' for windowed and 'f' for fullscreen. More options can be added easily, but it is also possible for you to accidentally call the function with an incorrect character (e.g. 'F' instead of 'f'). It is also less clear what 'w' and 'f' mean.

    This final possibility is the same as the third one. Just add
    Code:
    const char windowed = 'w';
    const char fullscreen = 'f';
    to the top of your current code with no other changes. This will make windowed and fullscreen actual variables (they aren't variables right now so the compiler complains).

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Ok, I just used the code that you suggested, I don't really mind using one letter, as it keeps things simple, and it works until I can figure out a 'better' way to do it

    Well, now I'm down to two errors, both relating to:

    Code:
    void SGE::UninitializeVideo()
    {
    	SDL_Quit();
    	exit(0);
    }
    First error:
    error: a function-definition is not allowed here before '{' token.
    Isn't it defined in the public section of class SGE?

    Second error:
    error: expected '}' at end of input.
    Still not quite sure I get this one... its there...

    Thanks again for your help!
    FlyingIsFun1217

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    ...
    
    if (SDL_Init(SDL_INIT_VIDEO) != 0) { /* Where's the matching brace?  ;) */ 
       		 fatalError("Could not initialize SDL Video.");
      	exit(1);
    
    	atexit(SDL_Quit);
    	
    	if (SM == windowed)
    	{
    		ScreenSurface = SDL_SetVideoMode (SSY, SSX, BPP);
    	}
    
    	else if (SM == fullscreen)
    	{
    		SDL_SetVideoMode (SSX, SSY, BPP, SDL_FULLSCREEN);
    	}
    }
    
    void SGE::UninitializeVideo()
    {
    	SDL_Quit();
    	exit(0);
    }

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Thanks

    It always seems to be the stupidest little things :P

    FlyingIsFun1217

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    #include	<cstdlib>
    
    #include <stdlib.h>
    I'll let you spot the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr: code & errors
    By reRanger in forum C++ Programming
    Replies: 12
    Last Post: 11-23-2004, 04:03 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. RAM/Swap Memory Code Errors
    By ghe1 in forum Linux Programming
    Replies: 2
    Last Post: 04-01-2002, 06:37 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21