快板/ c++:屏幕是空指针
Allegro/C++: Screen is null pointer
刚刚安装了Allegro 4.2.2,我想运行一个简单的程序。
当启动时,屏幕完全黑色,但输入正常响应键。我发现screen
是0x00000000 (nullptr)。无论如何,set_gfx_mode
没有返回错误。
MinGW根本不大喊大叫,没有警告,什么都没有。代码块只链接liballeg.a
和liballeg_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_W
或SCREEN_H
的值。
添加到#include lines之后:
#define SCREEN_W 640
#define SCREEN-h 480
当然,只要图形驱动程序/显卡支持,您可以将该值更改为您想要的任何屏幕大小。
相关文章:
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 运行时错误:引用绑定到类型为"int"的空指针
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 为什么我在空指针错误(链表)中获取成员访问权限
- 从向量到空指针的 memcpy(反之亦然)不起作用
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 成员访问是否在空指针上定义C++?
- 尝试将对象插入空指针数组时出现分段错误
- 为什么我们需要在 C++ 中检查空指针,而在 Java 中不需要?
- 是否允许向空指针添加零?
- 为什么 C 样式字符串的工作空指针检查不?
- 在函数内初始化无符号字符指针将返回空指针
- 为什么多维数组中的空字符串文本衰减为空指针?
- C++ 取消引用指向矢量的空指针时的分段错误
- 错误 C6011:取消引用空指针"NAME"。C++
- cppcheck取消引用空指针
- 打印空指针时,std::cout 可以打印 "NULL" 而不是 0 吗?
- 递增空指针无法正确设置值
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 快板/ c++:屏幕是空指针