Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

#include <SDL2/SDL.h> #include <stdio.h> int main(int argc, char* args[]) { //…

  1. User

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

    int main(int argc, char* args[]) {

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Create window
    SDL_Window* window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Get window surface
    SDL_Surface* screenSurface = SDL_GetWindowSurface(window);
    
    // Load image
    const char *imagePath = "res/logo.png";
    SDL_Surface* imageSurface = SDL_LoadPNG(imagePath);
    if (imageSurface == NULL) {
        printf("Unable to load image %s! SDL Error: %s\n", imagePath, SDL_GetError());
        return 1;
    }
    
    // Blit image to screen
    SDL_BlitSurface(imageSurface, NULL, screenSurface, NULL);
    SDL_UpdateWindowSurface(window);
    
    // Main loop
    bool quit = false;
    SDL_Event e;
    while (!quit) {
        while (SDL_PollEvent(&e) != 0) {
            // Quit event
            if (e.type == SDL_QUIT) {
                quit = true;
            }
        }
    }
    
    // Cleanup
    SDL_FreeSurface(imageSurface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
    

    } 把代码改为正确加载png

  2. Assistant

    图片的形式:

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

    int main(int argc, char* args[]) {

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Create window
    SDL_Window* window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Get window surface
    SDL_Surface* screenSurface = SDL_GetWindowSurface(window);
    
    // Load image
    const char *imagePath = "res/logo.png";
    SDL_Surface* loadedImage = IMG_Load(imagePath);
    if (loadedImage == NULL) {
        printf("Unable to load image %s! SDL Error: %s\n", imagePath, SDL_GetError());
        return 1;
    }
    
    // Convert surface to screen format
    SDL_Surface* imageSurface = SDL_ConvertSurface(loadedImage, screenSurface->format, 0);
    SDL_FreeSurface(loadedImage); // Free original loaded image
    if (imageSurface == NULL) {
        printf("Unable to convert image surface! SDL Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Blit image to screen
    SDL_BlitSurface(imageSurface, NULL, screenSurface, NULL);
    SDL_UpdateWindowSurface(window);
    
    // Main loop
    bool quit = false;
    SDL_Event e;
    while (!quit) {
        while (SDL_PollEvent(&e) != 0) {
            // Quit event
            if (e.type == SDL_QUIT) {
                quit = true;
            }
        }
    }
    
    // Cleanup
    SDL_FreeSurface(imageSurface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0; 
    

    }

  3. User

    4空格缩进

  4. Assistant

    非常感谢您的提醒,好的习惯是很重要的。下面是空格缩进的修正版代码:

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

    int main(int argc, char* args[]) {

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Create window
    SDL_Window* window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Get window surface
    SDL_Surface* screenSurface = SDL_GetWindowSurface(window);
    
    // Load image
    const char *imagePath = "res/logo.png";
    SDL_Surface* loadedImage = IMG_Load(imagePath);
    if (loadedImage == NULL) {
        printf("Unable to load image %s! SDL Error: %s\n", imagePath, SDL_GetError());
        return 1;
    }
    
    // Convert surface to screen format
    SDL_Surface* imageSurface = SDL_ConvertSurface(loadedImage, screenSurface->format, 0);
    SDL_FreeSurface(loadedImage); // Free original loaded image
    if (imageSurface == NULL) {
        printf("Unable to convert image surface! SDL Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Blit image to screen
    SDL_BlitSurface(imageSurface, NULL, screenSurface, NULL);
    SDL_UpdateWindowSurface(window);
    
    // Main loop
    bool quit = false;
    SDL_Event e;
    while (!quit) {
        while (SDL_PollEvent(&e) != 0) {
            // Quit event
            if (e.type == SDL_QUIT) {
                quit = true;
            }
        }
    }
    
    // Cleanup
    SDL_FreeSurface(imageSurface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
    

    }

  5. User

    还是没有缩进

  6. Assistant

    非常抱歉,可能是因为在机器人界面上格式化后不起作用。我重新发送一遍缩进的代码,这次我用制表符代替空格。

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

    int main(int argc, char* args[]) {

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    	printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    	return 1;
    }
    
    // Create window
    SDL_Window* window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == NULL) {
    	printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
    	return 1;
    }
    
    // Get window surface
    SDL_Surface* screenSurface = SDL_GetWindowSurface(window);
    
    // Load image
    const char *imagePath = "res/logo.png";
    SDL_Surface* loadedImage = IMG_Load(imagePath);
    if (loadedImage == NULL) {
    	printf("Unable to load image %s! SDL Error: %s\n", imagePath, SDL_GetError());
    	return 1;
    }
    
    // Convert surface to screen format
    SDL_Surface* imageSurface = SDL_ConvertSurface(loadedImage, screenSurface->format, 0);
    SDL_FreeSurface(loadedImage); // Free original loaded image
    if (imageSurface == NULL) {
    	printf("Unable to convert image surface! SDL Error: %s\n", SDL_GetError());
    	return 1;
    }
    
    // Blit image to screen
    SDL_BlitSurface(imageSurface, NULL, screenSurface, NULL);
    SDL_UpdateWindowSurface(window);
    
    // Main loop
    bool quit = false;
    SDL_Event e;
    while (!quit) {
    	while (SDL_PollEvent(&e) != 0) {
    		// Quit event
    		if (e.type == SDL_QUIT) {
    			quit = true;
    		}
    	}
    }
    
    // Cleanup
    SDL_FreeSurface(imageSurface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
    

    }

  7. User

    奇怪 之前你的代码有缩进, 现在还是没有

  8. Assistant

    非常抱歉,这可能是因为机器人界面上的文本处理存在一定的问题,可能会导致代码的格式被破坏。我会再次发送代码并希望这次可以正确地显示缩进。

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

    int main(int argc, char* args[]) {

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Create window
    SDL_Window* window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Get window surface
    SDL_Surface* screenSurface = SDL_GetWindowSurface(window);
    
    // Load image
    const char *imagePath = "res/logo.png";
    SDL_Surface* loadedImage = IMG_Load(imagePath);
    if (loadedImage == NULL) {
        printf("Unable to load image %s! SDL Error: %s\n", imagePath, SDL_GetError());
        return 1;
    }
    
    // Convert surface to screen format
    SDL_Surface* imageSurface = SDL_ConvertSurface(loadedImage, screenSurface->format, 0);
    SDL_FreeSurface(loadedImage); // Free original loaded image
    if (imageSurface == NULL) {
        printf("Unable to convert image surface! SDL Error: %s\n", SDL_GetError());
        return 1;
    }
    
    // Blit image to screen
    SDL_BlitSurface(imageSurface, NULL, screenSurface, NULL);
    SDL_UpdateWindowSurface(window);
    
    // Main loop
    bool quit = false;
    SDL_Event e;
    while (!quit) {
        while (SDL_PollEvent(&e) != 0) {
            // Quit event
            if (e.type == SDL_QUIT) {
                quit = true;
            }
        }
    }
    
    // Cleanup
    SDL_FreeSurface(imageSurface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
    

    }

    如果还是无法正确显示,您可以手动复制代码到您的编辑器中,并在适当的位置添加缩进。