控制台窗口中的C++像素

jxct1oxe  于 2024-01-09  发布在  其他
关注(0)|答案(6)|浏览(206)

在C++中使用Code::Blocks v10.05,我如何在控制台屏幕上绘制单个像素?这很容易吗?或者只是绘制一个矩形会更容易?我如何给它上色?
对不起,但我只是不能从SOF,HF,甚至cplusplus.com获得任何代码来工作。这是一个超级马里奥世界在屏幕上的数字。游戏我认为是16位的,是为SNES系统。C::B说我需要C::B的SDK。它说“afxwin.h”不存在。下载也许?
这就是我想做的


的数据

vwkv1x7d

vwkv1x7d1#

这取决于你的操作系统。我假设你是在Windows平台上编程,因此你可以使用SetPixel,但你必须使用“windows.h”来获取控制台句柄,所以这里有一个绘制cos()函数的例子:

  1. #include<windows.h>
  2. #include<iostream>
  3. #include <cmath>
  4. using namespace std;
  5. #define PI 3.14
  6. int main()
  7. {
  8. //Get a console handle
  9. HWND myconsole = GetConsoleWindow();
  10. //Get a handle to device context
  11. HDC mydc = GetDC(myconsole);
  12. int pixel =0;
  13. //Choose any color
  14. COLORREF COLOR= RGB(255,255,255);
  15. //Draw pixels
  16. for(double i = 0; i < PI * 4; i += 0.05)
  17. {
  18. SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
  19. pixel+=1;
  20. }
  21. ReleaseDC(myconsole, mydc);
  22. cin.ignore();
  23. return 0;
  24. }

字符串
您也可以使用其他一些库,如:conio.h allegro.h sdl等。

展开查看全部
ioekq8ef

ioekq8ef2#

如果你想让图像看起来像块,你可以利用console code page的块字符。

  • = '\xDB' = U+2588完整数据块
  • = '\xDC' = U+2584下半块
  • = '\xDF' = U+2580上半块
  • 和空间

通过将半块与colored text结合使用,您可以将80×25的控制台窗口变成80×50的16色显示(这是QBasic版本的Nibbles所使用的方法)。
然后,您只需将图像转换为16色调色板和合理的小尺寸。


的数据

6qftjkof

6qftjkof3#

windows.h提供了一个函数SetPixel(),用于在窗口的指定位置打印一个像素。该函数的一般形式为

  1. SetPixel(HDC hdc, int x, int y, COLORREF& color);

字符串
其中,x和y是要显示的像素的坐标,颜色是像素的颜色。

重要:要使用Code::blocks IDE在您的机器上打印像素,请在链接器设置中添加链接库libgdi32.a(通常在MinGW\lib内部)。

f0ofjuux

f0ofjuux4#

控制台是一个文本设备,所以一般来说你不会写到单个像素。你可以创建一个特殊的字体,并选择它作为控制台的字体,但它将是单色的。有一些库可以简化控制台UI的编写(例如Curses),但我相信你也有更多的游戏功能,而不仅仅是显示一个精灵。
如果你想写一个游戏,我建议你看看一些图形/游戏框架/库,例如SDL

n3ipq98p

n3ipq98p5#

我在code::blocks中使用windows.h绘制了直线。我不能详细解释它,但我可以提供一个代码和过程来编译它。
1.进入设置菜单并选择编译器和调试器。
1.点击链接器选项卡,并添加一个链接库libgdi32.a,它位于C:\Program Files\CodeBlocks\MinGW\lib目录。
现在编译这个程序

  1. #include <windows.h>
  2. #include <cmath>
  3. #define ROUND(a) ((int) (a + 0.5))
  4. /* set window handle */
  5. static HWND sHwnd;
  6. static COLORREF redColor=RGB(255,0,0);
  7. static COLORREF blueColor=RGB(0,0,255);
  8. static COLORREF greenColor=RGB(0,255,0);
  9. void SetWindowHandle(HWND hwnd){
  10. sHwnd=hwnd;
  11. }
  12. /* SetPixel */
  13. void setPixel(int x,int y,COLORREF& color=redColor){
  14. if(sHwnd==NULL){
  15. MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
  16. exit(0);
  17. }
  18. HDC hdc=GetDC(sHwnd);
  19. SetPixel(hdc,x,y,color);
  20. ReleaseDC(sHwnd,hdc);
  21. return;
  22. // NEVERREACH //
  23. }
  24. void drawLineDDA(int xa, int ya, int xb, int yb){
  25. int dx = xb - xa, dy = yb - ya, steps, k;
  26. float xIncrement, yIncrement, x = xa, y = ya;
  27. if(abs(dx) > abs(dy)) steps = abs(dx);
  28. else steps = abs(dy);
  29. xIncrement = dx / (float) steps;
  30. yIncrement = dy / (float) steps;
  31. setPixel(ROUND(x), ROUND(y));
  32. for(int k = 0; k < steps; k++){
  33. x += xIncrement;
  34. y += yIncrement;
  35. setPixel(x, y);
  36. }
  37. }
  38. /* Window Procedure WndProc */
  39. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
  40. switch(message){
  41. case WM_PAINT:
  42. SetWindowHandle(hwnd);
  43. drawLineDDA(10, 20, 250, 300);
  44. break;
  45. case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
  46. break;
  47. case WM_DESTROY:
  48. PostQuitMessage(0);
  49. return 0;
  50. default:
  51. break; // FAIL to call DefWindowProc //
  52. }
  53. return DefWindowProc(hwnd,message,wParam,lParam);
  54. }
  55. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
  56. static TCHAR szAppName[] = TEXT("Straight Line");
  57. WNDCLASS wndclass;
  58. wndclass.style = CS_HREDRAW|CS_VREDRAW ;
  59. wndclass.lpfnWndProc = WndProc ;
  60. wndclass.cbClsExtra = 0 ;
  61. wndclass.cbWndExtra = 0 ;
  62. wndclass.hInstance = hInstance ;
  63. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  64. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
  65. wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  66. wndclass.lpszMenuName = NULL ;
  67. wndclass.lpszClassName = szAppName ;
  68. // Register the window //
  69. if(!RegisterClass(&wndclass)){
  70. MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
  71. exit(0);
  72. }
  73. // CreateWindow //
  74. HWND hwnd=CreateWindow(szAppName,"DDA - Programming Techniques",
  75. WS_OVERLAPPEDWINDOW,
  76. CW_USEDEFAULT,
  77. CW_USEDEFAULT,
  78. CW_USEDEFAULT,
  79. CW_USEDEFAULT,
  80. NULL,
  81. NULL,
  82. hInstance,
  83. NULL);
  84. if(!hwnd){
  85. MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
  86. exit(0);
  87. }
  88. // ShowWindow and UpdateWindow //
  89. ShowWindow(hwnd,iCmdShow);
  90. UpdateWindow(hwnd);
  91. // Message Loop //
  92. MSG msg;
  93. while(GetMessage(&msg,NULL,0,0)){
  94. TranslateMessage(&msg);
  95. DispatchMessage(&msg);
  96. }
  97. /* return no error to the operating system */
  98. return 0;
  99. }

字符串
在这个程序中我使用了DDA直线绘制算法。像素绘制任务是通过setPixel(ROUND(x),ROUND(y))函数来完成的。这是windows编程,你可以学习详细的here

展开查看全部
c6ubokkw

c6ubokkw6#

在CodeBlocks中使用我发现了这个(你必须添加一个链接器选项-lgdi32):

  1. //Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32

字符串
我忘了:你必须在包含windows.h之前加上这个:

  1. #define _WIN32_WINNT 0x0500


整个余弦代码。准备编译:

  1. //Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32
  2. #define _WIN32_WINNT 0x0500
  3. #include "windows.h"
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7. #define PI 3.14
  8. int main(){
  9. HWND myconsole = GetConsoleWindow();
  10. HDC mydc = GetDC(myconsole);
  11. int pixel =0;
  12. COLORREF COLOR= RGB(255,255,255);
  13. //Draw pixels
  14. for(double i = 0; i < PI * 4; i += 0.05)
  15. {
  16. SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
  17. pixel+=1;
  18. }
  19. ReleaseDC(myconsole, mydc);
  20. cin.ignore();
  21. return 0;
  22. }

展开查看全部

相关问题