简体   繁体   English

Allegro / C ++:屏幕为空指针

[英]Allegro/C++: Screen is null pointer

Just installed Allegro 4.2.2, and I'm trying to run a simple program. 刚刚安装了Allegro 4.2.2,并且我正在尝试运行一个简单的程序。

When launched, screen is totally black, but input normally reacts to keys. 启动后,屏幕将全黑,但输入通常会对按键做出反应。 I found that screen is 0x00000000 (a nullptr). 我发现screen是0x00000000(一个nullptr)。 Anyway, set_gfx_mode returns no error. 无论如何, set_gfx_mode返回错误。

MinGW doesn't yell at all, no warnings, nothing. MinGW一点也不喊,没有警告,什么也没有。 Code Blocks has linked only liballeg.a and liballeg_s.a with other options: 代码块仅将liballeg.aliballeg_s.a与其他选项链接:

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

Here's the code: 这是代码:

#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: %s\n", allegro_error );
    }
    BITMAP* buffer = create_bitmap_ex( 32, SCREEN_W, SCREEN_H );
    printf( "Screen location: %p\n", 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()

It is a very simple error, you are not defining anywhere on your code what is the value of SCREEN_W nor SCREEN_H . 这是一个非常简单的错误,您没有在代码的任何地方定义SCREEN_WSCREEN_H的值是什么。

Add this after the #include lines: #include行之后添加:

#define SCREEN_W 640
#define SCREEN-h 480

Of course, you can change the value to whatever screen size you want, as long as the graphics driver/card supports it. 当然,您可以将值更改为所需的任何屏幕尺寸,只要图形驱动程序/卡支持它即可。

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

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