快板/ c++:屏幕是空指针

Allegro/C++: Screen is null pointer

本文关键字:空指针 屏幕 c++ 快板      更新时间:2023-10-16

刚刚安装了Allegro 4.2.2,我想运行一个简单的程序。

当启动时,屏幕完全黑色,但输入正常响应键。我发现screen是0x00000000 (nullptr)。无论如何,set_gfx_mode没有返回错误。

MinGW根本不大喊大叫,没有警告,什么都没有。代码块只链接liballeg.aliballeg_s.a与其他选项:

-lalleg_s -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lole32 -ldinput -lddraw -ldxguid -lwinmm -ldsound

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>
#include <time.h>

int main() {
    srand( time( NULL ) );
    allegro_init();
    // screen
    set_color_depth( 32 );
    if ( set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 ) < 0 ) {
        printf( "Error set_gfx_mode: %sn", allegro_error );
    }
    BITMAP* buffer = create_bitmap_ex( 32, SCREEN_W, SCREEN_H );
    printf( "Screen location: %pn", screen );
    // keyboard and mouse
    install_keyboard();
    install_mouse();
    show_os_cursor( 1 );
    // main loop
    bool done = false;
    while ( !done ) {
        // keys
        if ( keypressed() ) {
            int keyID = readkey() & 0xFF;
            switch ( keyID ) {
                case 27: { // escape
                    done = true;
                    break;
                }
            }
        }
        // rendering
        clear_bitmap( buffer );
        rectfill( buffer, 50, 50, 150, 150, 0x00FFFFFF );
        blit( buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
    }
    destroy_bitmap( buffer );
    allegro_exit();
    return 0;
}
END_OF_MAIN()

这是一个非常简单的错误,您没有在代码的任何地方定义SCREEN_WSCREEN_H的值。

添加到#include lines之后:

#define SCREEN_W 640
#define SCREEN-h 480

当然,只要图形驱动程序/显卡支持,您可以将该值更改为您想要的任何屏幕大小。