SDL2 - 'SDL_HWSURFACE'未声明(首次使用此功能)

时间:2017-08-25 09:14:29

标签: c 64-bit mingw sdl sdl-2

我用旧版SDL 1.2获得了C语言的现有旧项目, 我试图让它运行并在安装SDL之后,我发现我的PC是64位因此我需要新的SDL2,在重新安装正确的SDL2后,我配置了包含行指向SDL2文件夹而不是旧的SDL,当我尝试运行时,我收到2个错误:

1 - 'SDL_HWSURFACE'未声明(首次使用此功能)

2 - 'SDL_SetColorKey'未声明(首次使用此功能)

PS - 使用MINGW编译器

任何想法?

我正在附加代码,但它很长......

/*
 * GuiFramework.c
 *
 *  Created on: 24 áñôè 2014
 *      Author: Ilias
 */

#include "GuiFramework.h"

static Control* root = NULL;
static SDL_Surface* surface = NULL;
static SDL_Surface* image = NULL;
static int click_x, click_y;
static Control* foundButton = NULL;
static int quit = FALSE;
static int enabled = FALSE;
static int visible = FALSE;
static int onlyEnabled = TRUE;

Control* allocControl() {

    Control* control = malloc(sizeof(Control));

    if (control == NULL) {
        perror_message("malloc");
        freeControls(root);
        exit(0);
    }

    control->children = NULL;
    control->parent = NULL;
    control->next = NULL;
    control->clip = NULL;
    control->onClick = NULL;
    control->onDraw = NULL;
    control->enabled = TRUE;
    control->visible = TRUE;

    return control;
}

SDL_Rect* createRect(int x, int y, int w, int h) {
    SDL_Rect* rect = malloc(sizeof(SDL_Rect));

    if (rect == NULL) {
        perror_message("malloc");
        freeControls(root);
        exit(0);
    }

    rect->w = w;
    rect->h = h;
    rect->x = x;
    rect->y = y;

    return rect;
}

Control* createButton(Control* parent, SDL_Rect* dim, SDL_Rect* clip, void (*func)(Control*), int enabled) {

    Control* control = allocControl();
    appendControl(parent, control);

    control->type = BUTTON;
    control->dim = dim;
    control->abs = createRect(parent->abs->x + dim->x, parent->abs->y + dim->y, dim->w, dim->h);
    control->clip = clip;
    control->onClick = func;
    control->visible = parent->visible;
    control->enabled = enabled && control->visible;

    return control;
}

Control* createPanel(Control* parent, SDL_Rect* dim, void (*func)(Control*), int visible) {

    Control* control = allocControl();
    appendControl(parent, control);

    control->type = PANEL;
    control->dim = dim;
    control->abs = createRect(parent->abs->x + dim->x, parent->abs->y + dim->y, dim->w, dim->h);
    control->onDraw = func;
    control->visible = parent->visible && visible;

    return control;
}

Control* createImage(Control* parent, SDL_Rect* dim, SDL_Rect* clip) {

    Control* control = allocControl();
    appendControl(parent, control);

    control->type = IMAGE;
    control->dim = dim;
    control->abs = createRect(parent->abs->x + dim->x, parent->abs->y + dim->y, dim->w, dim->h);
    control->clip = clip;
    control->visible = parent->visible;

    return control;
}

Control* createWindow(int width, int height, char* caption) {

    Control* control = allocControl(NULL);

    control->type = WINDOW;
    control->dim = createRect(0, 0, width, height);
    control->abs = createRect(0, 0, width, height);

    /* Initialize the surface for resize issues */
    if (surface != NULL) {
        SDL_FreeSurface(surface);
    }

    surface = SDL_SetVideoMode(width, height, 0, SDL_HWSURFACE);

    if (surface == NULL) {
        printf("ERROR: failed to set video mode: %s\n", SDL_GetError());
        freeControls(root);
        exit(0);
    }

    /* Color the window background */
    colorRect(control->dim, 0xFF, 0xFF, 0xFF);

    SDL_WM_SetCaption(caption, caption);

    return control;
}

void appendControl(Control* parent, Control* child) {
    Control* currChild;
    child->parent = parent;

    /* Appends the control to the end list of the children of parent */
    if (parent->children == NULL) {
        parent->children = child;
    } else {
        currChild = parent->children;

        while (currChild->next != NULL) {
            currChild = currChild->next;
        }

        currChild->next = child;
    }
}

void initializeSDL() {

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("ERROR: unable to init SDL: %s\n", SDL_GetError());
        freeControls(root);
        exit(0);
    }

    atexit(SDL_Quit);
}

void loadImage(char* name) {

    SDL_Surface* loadedImage = SDL_LoadBMP(name);

    if (loadedImage == NULL) {
        printf("ERROR: unable to load image: %s\n", SDL_GetError());
        exit(0);
    }

    image = loadedImage;
}

void colorRect(SDL_Rect* rect, int r, int g, int b) {
    if (SDL_FillRect(surface, rect, SDL_MapRGB(surface->format, r, g, b)) < 0) {
        printf("ERROR: failed to fill rect with color: %s\n", SDL_GetError());
        freeControls(root);
        exit(0);;
    }
}

void drawBorders(SDL_Rect* rect, int width, int r, int g, int b) {

    int i;

    SDL_Rect* borderClips[4];
    borderClips[0] = createRect(rect->x - width, rect->y - width, rect->w + 2 * width, width);
    borderClips[1] = createRect(rect->x - width, rect->y - width, width, rect->h + 2 * width);
    borderClips[2] = createRect(rect->x + rect->w, rect->y - width, width, rect->h + 2 * width);
    borderClips[3] = createRect(rect->x - width, rect->y + rect->h, rect->w + 2 * width, width);

    /* Draw Borders around given rect */
    for (i = 0; i < 4; i++) {
        colorRect(borderClips[i], r, g, b);
    }

    free(borderClips[0]);
    free(borderClips[1]);
    free(borderClips[2]);
    free(borderClips[3]);
}

void optimizeImage() {

    Uint32 colorKey;
    SDL_Surface* optimizedImage = SDL_DisplayFormat(image);
    SDL_FreeSurface(image);

    if (optimizedImage == NULL) {
        printf("ERROR: unable to optimize image: %s\n", SDL_GetError());
        freeControls(root);
        exit(0);
    }

    /* RGB: 250,255,255 is the key color */
    colorKey = SDL_MapRGB(optimizedImage->format, 0xFA, 0xFF, 0xFF);
    SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorKey);

    image = optimizedImage;
}

void freeControls(Control* root) {
    traverseTree(root, freeControl, TRUE);

    if (quit) {
        SDL_FreeSurface(image);
    }
}

int freeControl(Control* control) {

    if (control->dim != NULL) {
        free(control->dim);
    }

    if (control->abs != NULL) {
        free(control->abs);
    }

    free(control);
    return 1;
}

void drawRoot() {
    traverseTree(root, drawControl, FALSE);

    /* After controls are drawn we flip */
    if (SDL_Flip(surface) < 0) {
        printf("ERROR: unable to flip screen: %s\n", SDL_GetError());
        freeControls(root);
        exit(0);
    }
}

int drawControl(Control* control) {

    if (control->visible) {

        /* Draw if there is an onDraw function (used mostly by panels) */
        if (control->onDraw != NULL) {
            control->onDraw(control);
        }

        /* Draw also if there is an image to draw */
        if (control->clip != NULL) {
            if (SDL_BlitSurface(image, control->clip, surface, control->abs) < 0) {
                printf("ERROR: unable to blit surface: %s\n", SDL_GetError());
                freeControls(root);
                exit(0);
            }
        }
    }

    return 1;
}

int setControlEnabled(Control* control) {

    control->enabled = enabled && control->visible;

    return 1;
}

int setControlVisible(Control* control) {

    control->visible = visible;

    return 1;
}

int findButton(Control* control) {

    /* Check only if control is clickable */
    if (control->onClick != NULL) {
        if (click_x >= control->abs->x && click_x < control->abs->x + control->abs->w &&
            click_y >= control->abs->y && click_y < control->abs->y + control->abs->h) {

            if (!onlyEnabled || control->enabled) {

                /* Found a button in the area, no need to keep looping */
                foundButton = control;
                return 0;
            }
        }
    }

    return 1;
}

int traverseTree(Control* control, int(*func)(Control*), int postOrder) {
    Control* child;
    Control* next;
    int keepLooping = 1;
    if (control != NULL) {

        /* Perform func before recursion if not post order */
        if (!postOrder && keepLooping) {
            keepLooping = func(control);
        }

        /* Recurse the tree */
        child = control->children;
        while (child != NULL && keepLooping) {
            next = child->next;
            keepLooping = traverseTree(child, func, postOrder);
            child = next;
        }

        /* Perform func after recursion if post order */
        if (postOrder && keepLooping) {
            keepLooping = func(control);
        }
    }

    return keepLooping;
}

void handleEvents() {

    SDL_Event e;

    while (!quit) {
        if (SDL_PollEvent(&e) != 0) {
            if (e.type == SDL_QUIT) { /* A quit was detected */
                quitGuiLoop();
            } else if (e.type == SDL_MOUSEBUTTONDOWN) { /* Mouse click was detected */

                if (e.button.button == SDL_BUTTON_LEFT) {
                    click_x = e.button.x;
                    click_y = e.button.y;

                    /* Find the button that was pressed */
                    onlyEnabled = TRUE;
                    traverseTree(root, findButton, TRUE);

                    if (foundButton != NULL) {
                        foundButton->onClick(foundButton);
                        foundButton = NULL;
                    }
                }
            }

            drawRoot();
            delay(10);
        }
    }
}

void delay(int ms) {
    SDL_Delay(ms);
}

void quitGuiLoop() {
    quit = 1;
}

Control* getControlByDimension(int x, int y) {

    Control* control;
    click_x = x;
    click_y = y;

    /* Find the button located at specified coordinates */
    onlyEnabled = FALSE;
    traverseTree(root, findButton, TRUE);

    control = foundButton;
    foundButton = NULL;

    return control;
}

Control* getRoot() {
    return root;
}

void setRoot(Control* newRoot) {
    root = newRoot;
}

void setControlsVisible(Control* root, int visibleValue) {
    visible = visibleValue;
    traverseTree(root, setControlVisible, TRUE);
}

void setControlsEnabled(Control* root, int enabledValue) {
    enabled = enabledValue;
    traverseTree(root, setControlEnabled, TRUE);
}

0 个答案:

没有答案