开发者

nesting structures, interfaces, and classes within an interface

开发者 https://www.devze.com 2023-03-07 18:42 出处:网络
I\'m actually translating some C++ code (which I know very little about, and have never really used) to C#. Normally in C# I wouldn\'t find myself doing something like this, as it does seem a little o

I'm actually translating some C++ code (which I know very little about, and have never really used) to C#. Normally in C# I wouldn't find myself doing something like this, as it does seem a little odd, but with the way the code in C++ is setup, I find it hard not to do it this way. Admittedly, I'm not very experienced with programming at all, but for the amount of time I've been doing it I've been able to grasp the concepts fairly well.

Anyway, here's the C++ code. It's in a header file, too.

#ifndef _SPRITE_H_
#define _SPRITE_H_

#ifdef __cplusplus
    extern "C" {
#endif /* __cplusplus */


#ifndef NULL
    #define NULL ((void *) 0)
#endif

typedef struct {
    unsigned char *data;
    int len;
    int width;
    int height;
} SpriteImage;


typedef struct {
    unsigned char b;
    unsigned char g;
    unsigned char r;
    unsigned char unused;
} SpritePalette;


typedef struct {
    char *filename;
    unsigned int nimages;
    SpriteImage *images;
    unsigned int palette_size;
    SpritePalette *palette;
} Sprite;


typedef enum {
    /* Developer errors */
    SE_BADARGS,

    /* sprite_new() errors */
    SE_CANTOPEN,
    SE_INVALID,

    /* sprite_to_bmp(), sprite_to_bmp_file() and sprite_to_rgb() errors */
    SE_INDEX,

    /* sprite_to_bmp_file() errors */
    SE_CANTWRITE
} SpriteError;

//Funcion para hacer uso de reverse_palette desde el exterior
SpritePalette * get_pal(SpritePalette *palette,int palette_len);

/* Open sprite file */
Sprite *sprite_open (const char *fname, SpriteError *error);

Sprite *sprite_open_from_data (const unsigned char *data, unsigned int size, SpriteError *error);

/* Change palette of sprite*/
void change_palete(Sprite *sprite, const char *fname, SpriteError *error);

/* Converts a sprite to bitmap file in memory */
void *sprite_to_bmp (Sprite *sprite, int i, int *size, SpriteError *error);

/* Like sprite_to_bmp(), but saves the result to a file */
int sprite_to_bmp_file (Sprite *sprite, int i, const char *writeToFile, SpriteError *error);

/* Converts a sprite to raw RGB data. The rowstride/pitch is 3*width. */
void *sprite_to_rgb (Sprite *sprite, int i, int *size, SpriteError *error);

/* Frees a Sprite* pointer */
void sprite_free (Sprite *sprite);


#ifdef __cplusplus
    }
#endif /* __cplusplus */

#endif /* _SPRITE_H_ */

By the way, does anyone know what the deal is with the '#' reference?

I have no idea what these refer to.

And here's the C#:

interface Sprite
 {
    public class SpriteImage
    {
        private byte *data;
        private int length;
        private int width;
        private int height;
    }

    public class SpritePalette
    {
        byte b;
        byte g;
        byte r;
        byte unused;
    }

    public class Sprite
    {
        string fileName;
        uint nImages;
        u开发者_StackOverflow社区int palette_size;
        SpriteImage image;
        SpritePalette palette; 
    }

    public enum SpriteErrors
    { 
        None, //--default value
        BadArguments, //--dev errors

        /*--errors derived from any instance/call of the NewSprite() method */
        CantOpen, 
        Invalid,

        /*SpriteToBMP(), SpriteToBMPFile(), and SpriteToRGB() errors*/
        Index,

        CantWrite //--SpriteToBMPFile() errors 
    }

    public interface ISprite
    {
        SpritePalette GetPalette(SpritePalette palette, int paletteLength);

        Sprite SpriteOpen(string firstName, SpriteErrors* error);

        Sprite SpriteOpenFromData(byte* data, uint size, SpriteErrors* error);


    } 

I'm sure you can connect the dots here. Keep in mind that this isn't my code, obviously, so I don't really know much about it. If anyone needs anymore material though I'd be happy to provide it if necessary.


a couple of points:
1) there is your types shouldn't be inside of an interface
2) pointers should either be converted to memebers as you hve in the sprite class or to arrays as you should have in the SpriteImage struct
3) unless its a trivial port it is going to be very difficult to write without having a good understanding of both languages and the code to be ported


You appear to be trying to port this SourceForge project from C++ to C#:

  • Ragnarok Online Sprite Viewer

That viewer is not only written in C++ but is also based on the Qt Toolkit.

I know your question is about this translating this particular header file from C++ to C# and what is the best approach, but my opinion is that unless you are very comfortable with C++ and are willing to learn a lot about Qt, your chances of success at this porting project are not very good. This is a big project even for a programmer seasoned in both C++ and C#.

However, if you still want to do this thing, then the approach you should take is to create a single large SpriteUtility static class and put all of the free C++ functions you find into that class as static C# methods. Yes, you can also put the C++ structs you see as nested classes. You don't need any interfaces whatsoever.

It doesn't need to be beautiful C# code; you are trying to port it verbatim doing as little damage to it as possible. Once it is working you can refactor it to make it more object-oriented in the traditional C# style.

0

精彩评论

暂无评论...
验证码 换一张
取 消