Thread: C++ to C

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    48

    C++ to C

    I need to make a game in C. I'm using SDL but all the tutorials I've see of it are in C++. So I was wondering, how I can do this part in C:

    Code:
    //Texture wrapper class
    class LTexture
    {
        public:
            //Initializes Variables
            LTexture();
    
            //Deallocate Memory
            ~LTexture();
    
            //Loads image on specified path
            bool loadFromFile( std::string path ); 
    
            //Deallocate Texture
            void free();
    
            //Color Modulation
            void setColor( Uint8 red, Uint8 green, Uint8 blue );
    
            //Set blending
            void setBlendMode( SDL_BlendMode blending );
    
            //Set alpha modulation
            void setAlpha( Uint8 alpha );
    
            //Renders texture on give point
            void render( int x, int y, SDL_Rect* clip = NULL );
    
            //Gets image dimension
            int getWidth();
            int getHeight();
    
        private:
            //Hardware actual texture
            SDL_Texture* mTexture;
    
            //Image Dimension
            int mWidth;
            int mHeight;
    };
    ////////////////////////////////////////////////////////////////
    LTexture::LTexture()
    {
        //Initialize
        mTexture = NULL;
        mWidth = 0;
        mHeight = 0;
    }
    I'm confused with the class part.
    Last edited by MrPecanha; 06-23-2016 at 11:54 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    make a struct called LTexture, containing the items below the private: line.

    for LTexture::LTexture(), write a function called LTexture_Create(), that returns an LTexture*.

    for each of the member functions, write functions such as LTexture_loadFromFile(const char *path), etc.

    for ~LTexture(), write a function called LTexture_Destroy().
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    So, I'm also a beginner at C so I'm probably messing other stuff up:

    What other changes do I have to do here:

    Code:
    //Texture wrapper functions
    //Initializes Variables
    LTexture_Create();
    
    //Dealocates Memories
    LTexture_Destroy();
    
    //Loads image on specified path
    bool loadFromFile( const char *path );
    
    //Deallocate texture
    void free();
    
    //Color Modulation
    void setColor( Uint8 red, Uint8 green, Uint8 blue );
    
    //Set blending
    void setBlendMode( SDL_BlendMode blending );
    
    //Set alpha modulation
    void setAlpha( Uint8 alpha );
    
    //Renders texture on given point
    void render( int x, int y, SDL_Rect* clip = NULL );
    
    //Gets image dimension
    int getWidth();
    int getHeight();
    
    ////////////////////////////////////////////////////////////////
    
    
    typedef struct
    {
        //Actual Hardware Texture
        SDL_Texture* mTexture;
    
        //Image Dimension
        int mWidth;
        int mHeight;
    
    } LTexture;
    
    //////////////////////////////////////////////////////////////////
    
    
    int LTexture_Create( LTexture *ltexture)
    {
        //Initialize
        mTexture = NULL;
        mWidth = 0;
        mHeight = 0;
        return LTexture*;
    }
    
    void LTexture_Destroy()
    {
        //Deallocate
        free();
    }
    
    bool loadFromFile(LTexture *ltexture, const char *path )
    {
    //...////////////////////////////
    }
    
    void free( LTexture *ltexture)
    {
        //Free texture if it exists
        if( mTexture != NULL )
        {
            SDL_DestroyTexture( mTexture );
            mTexture = NULL;
            mWidth = 0;
            mHeight = 0;
        }
    }
    
    void LsetColor( Uint8 red, Uint8 green, Uint8 blue )
    {
        //Modulate texture rgb
        SDL_SetTextureColorMod( mTexture, red, green, blue );
    }
    
    void setBlendMode( SDL_BlendMode blending )
    {
        //Set blending function
        SDL_SetTextureBlendMode( mTexture, blending );
    }
    
    void setAlpha( Uint8 alpha )
    {
        //Modulate texture alpha
        SDL_SetTextureAlphaMod( mTexture, alpha );
    }
    
    void render( int x, int y, SDL_Rect* clip, LTexture *ltexture )
    {
        //Set rendering space and render to screen
        SDL_Rect renderQuad = { x, y, mWidth, mHeight };
    
        //Set clip rendering dimensions
        if( clip != NULL )
        {
            renderQuad.w = clip->w;
            renderQuad.h = clip->h;
        }
    
        //Render to screen
        SDL_RenderCopy( gRenderer, mTexture, clip, &renderQuad );
    }
    
    int getWidth(LTexture *ltexture)
    {
        return mWidth;
    }
    
    int getHeight(LTexture *ltexture)
    {
        return mHeight;
    }
    
    ///////////////////
    One thing I'm confused in : In C++, the functions are stated first then later stated with arguments and code inside them . Why ?
    Last edited by MrPecanha; 06-23-2016 at 07:43 PM.

  4. #4
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Code:
    ////////////////////////////////////////////////////////////////////
    
    
    ////////////////////////////////////////////////////////////////////
    
    typedef struct{
        //Textura real do hardware
        SDL_Texture* mTexture;
    
        //Dimensão da imagem
        int mWidth;
        int mHeight;
    
    };LTexture;
    
            LTexture_Create();
    
            //Deallocate memory
            LTexture_Destroy();
    
            //Loads image on specified path
            bool LTexture_loadFromFile( const char *path );
    
            //Deallocate texture
            void LTexture_free();
    
            //Color Modulation
            void LTexture_setColor( Uint8 red, Uint8 green, Uint8 blue );
    
            //Set blending
            void LTexture_setBlendMode( SDL_BlendMode blending );
    
            //Set alpha modulation
            void LTexture_setAlpha( Uint8 alpha );
    
            //Renders texture on given point
            void LTexture_render( int x, int y, SDL_Rect* clip = NULL );
    
            //Gets image dimension
            int LTexture_getWidth();
            int LTexture_getHeight();
    
    LTexture_Create(LTexture *texture)
    {
        //Initialize
        texture->mTexture = NULL;
        texture->mWidth = 0;
        texture->mHeight = 0;
    }
    
    LTexture_Destroy()
    {
        //Deallocate
        free();
    }
    
    bool LTexture_loadFromFile(LTexture *texture; const char *path )
    {
        //Get rid of preexisting texture
        free();
    
        //The final texture
        SDL_Texture* newTexture = NULL;
    
        //Load image at specified path
        SDL_Surface* loadedSurface = IMG_Load( path );
        if( loadedSurface == NULL )
        {
            printf( "Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError() );
        }
        else
        {
            //Color key image
            SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0x69, 0 ) );
    
            //Create texture from surface pixels
            newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
            if( newTexture == NULL )
            {
                printf( "Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError() );
            }
            else
            {
                //Get image dimensions
                texture->mWidth = loadedSurface->w;
                texture->mHeight = loadedSurface->h;
            }
    
            //Get rid of old loaded surface
            SDL_FreeSurface( loadedSurface );
        }
    
        //Return success
        texture->mTexture = newTexture;
        return texture.mTexture != NULL;
    }
    
    void LTexture_free(LTexture *texture)
    {
        //Free texture if it exists
        if( texture.mTexture != NULL )
        {
            SDL_DestroyTexture( texture.mTexture );
            texture->mTexture = NULL;
            texture->mWidth = 0;
            texture->mHeight = 0;
        }
    }
    
    void LTexture_setColor(LTexture *texture, Uint8 red, Uint8 green, Uint8 blue )
    {
        //Modulate texture rgb
        SDL_SetTextureColorMod( texture.mTexture, red, green, blue );
    }
    
    void LTexture_setBlendMode(LTexture *texture, SDL_BlendMode blending )
    {
        //Set blending function
        SDL_SetTextureBlendMode( texture.mTexture, blending );
    }
    
    void LTexture_setAlpha(LTexture *texture, Uint8 alpha )
    {
        //Modulate texture alpha
        SDL_SetTextureAlphaMod( texture.mTexture, alpha );
    }
    
    void LTexture_render(LTexture *texture; int x, int y, SDL_Rect* clip )
    {
        //Set rendering space and render to screen
        SDL_Rect renderQuad = { x, y,texture.mWidth, texture.mHeight };
    
        //Set clip rendering dimensions
        if( clip != NULL )
        {
            renderQuad.w = clip->w;
            renderQuad.h = clip->h;
        }
    
        //Render to screen
        SDL_RenderCopy( gRenderer, texture.mTexture, clip, &renderQuad );
    }
    
    int LTexture_getWidth(LTexture *texture)
    {
        return texture.mWidth;
    }
    
    int LTexture_getHeight(LTexture *texture)
    {
        return texture.mHeight;
    }
    
    /////////////////////////////////////////////////////////////////////
    I believe this is a better attempt, but stuff just seem to get worse. How can I fix this ?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I haven't looked thoroughly into your code, but I think your main problem is that you're using "." instead of "->" for "texture".
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    48
    Quote Originally Posted by GReaper View Post
    I haven't looked thoroughly into your code, but I think your main problem is that you're using "." instead of "->" for "texture".
    Still Showing some errors after changing.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to mimic the 'this' argument, which C++ hides from you
    Code:
    void LTexture_Create(LTexture *this) {
      this->mTexture = 0;
      // etc
    }
    
    void LTexture_Destroy(LTexture *this) {
      free(this);
    }
    
    LTexture* LTexture_new( ) {
      return malloc(sizeof(LTexture));
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by MrPecanha View Post
    Still Showing some errors after changing.
    Please post the entire program you're trying to compile. Also, quote the error messages exactly with references to which line(s) produce the first error.

Popular pages Recent additions subscribe to a feed

Tags for this Thread