简体   繁体   English

如何在OSX下的SDL / OpenGL应用程序中加载JPG / PNG纹理

[英]How to load JPG/PNG Textures in an SDL/OpenGL App under OSX

i am writing an SDL / OpenGL application that runs under OSX. 我正在编写一个在OSX下运行的SDL / OpenGL应用程序。 I have to use existing code which uses the DevIL library for loading JPG and PNG textures. 我必须使用现有的代码,它使用DevIL库来加载JPG和PNG纹理。 Unfortunately, this works very bad under OS X, so i decided not to use DevIL at all, and rewrite the respective parts of the application using another library. 不幸的是,这在OS X下非常糟糕,所以我决定根本不使用DevIL,并使用另一个库重写应用程序的各个部分。 I want to keep it flexible (DevIL can handle a lot of image formats) and easy to use. 我想保持它的灵活性(DevIL可以处理很多图像格式)并且易于使用。 Is there a good replacement for DevIL that you can recommend? 你可以推荐一个很好的替代DevIL吗? The application is entirely written in C++. 该应用程序完全用C ++编写。

Have a look at the SDL_image library. 看看SDL_image库。 It offers functions like IMG_LoadPNG that load your picture "as an" SDL_Surface. 它提供了像IMG_LoadPNG这样的功能,可以将您的图片“加载”为SDL_Surface。 Since you already work with SDL this should fit quite well in your program. 由于您已经使用过SDL,因此这应该非常适合您的程序。

Sample taken from the SDL_image documentation : SDL_image文档中获取的示例

// Load sample.png into image
SDL_Surface* image = IMG_Load("sample.png");
if (image == nullptr) {
    std::cout << "IMG_Load: " << IMG_GetError() << "\n";
}

SDL 2 SDL_image minimal runnable example SDL 2 SDL_image最小可运行示例

main.c main.c中

#include <stdlib.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

int main(void) {
    SDL_Event event;
    SDL_Renderer *renderer = NULL;
    SDL_Texture *texture = NULL;
    SDL_Window *window = NULL;

    SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(
        500, 500,
        0, &window, &renderer
    );
    IMG_Init(IMG_INIT_PNG);
    texture = IMG_LoadTexture(renderer, "flower.png");
    while (1) {
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
            break;
    }
    SDL_DestroyTexture(texture);
    IMG_Quit();
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
}

GitHub upstream . GitHub上游

Compile and run: 编译并运行:

sudo apt-get install libsdl2-dev libsdl2-image-dev
gcc -std=c99 -o main -Wall -Wextra -pedantic main.c -lSDL2 -lSDL2_image
./main

Outcome: 结果:

在此输入图像描述

Tested on Ubuntu 16.04, GCC 6.4.0, SDL 2.0.4, SDL Image 2.0.1. 在Ubuntu 16.04,GCC 6.4.0,SDL 2.0.4,SDL Image 2.0.1上测试。

Take a look at freeimage . 看看freeimage It supports all major formats and is easily built with macports. 它支持所有主要格式,并且可以使用macport轻松构建。 Nice to work with as well. 很高兴也能合作。 Auto-detects image format etc. 自动检测图像格式等

FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename.c_str(), 0);
FIBITMAP *bitmap = FreeImage_Load(format, filename.c_str());
if (!bitmap)
{
    LOG_ERROR("Unable to load texture: " + filename);
    return false;
}
mlWidth = FreeImage_GetWidth(bitmap);
mlHeight = FreeImage_GetHeight(bitmap);
glGenTextures(1, &mpTextures[0]);
glBindTexture(GL_TEXTURE_2D, mpTextures[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mlWidth, mlHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE,
       (GLvoid*)FreeImage_GetBits(bitmap));
FreeImage_Unload(bitmap);

If you're on Mac OS anyway, why not just use CGImageSource to do the loading? 如果您还在使用Mac OS,为什么不使用CGImageSource进行加载呢? OS X natively supports loading many file formats including PNG and JPEG. OS X本身支持加载许多文件格式,包括PNG和JPEG。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM