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

时间:2012-09-11 22:07:31

标签: c++ console codeblocks pixels

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

Image I'm trying to create

5 个答案:

答案 0 :(得分:16)

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

#include<windows.h>
#include<iostream>
#include <cmath>

using namespace std;

#define PI 3.14

int main() 
{
    //Get a console handle
    HWND myconsole = GetConsoleWindow();
    //Get a handle to device context
    HDC mydc = GetDC(myconsole);

    int pixel =0;

    //Choose any color
    COLORREF COLOR= RGB(255,255,255); 

    //Draw pixels
    for(double i = 0; i < PI * 4; i += 0.05)
    {
        SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
        pixel+=1;
    }

    ReleaseDC(myconsole, mydc);
    cin.ignore();
    return 0;
}

您还可以使用其他一些库,例如:conio.h allegro.h sdl等。

答案 1 :(得分:12)

如果您愿意让图片看起来像块一样,您可以利用console code page中的阻止字符。

  • ='\ xDB'= U + 2588 FULL BLOCK
  • ='\ xDC'= U + 2584 LOWER HALF BLOCK
  • ='\ xDF'= U + 2580 UPPER HALF BLOCK
  • 和空间

通过将半块与colored text组合使用,您可以将80×25控制台窗口转换为80×50 16色显示屏。 (这是Nibbles的QBasic版本使用的方法。)

然后,您只需要将图像转换为16色调色板,并且尺寸相当小。

Mario in 8 lines and 10 columns of "text"

答案 2 :(得分:1)

控制台是文本设备,因此通常不会写入单个像素。您可以创建一个特殊字体并将其选为控制台的字体,但它将是单色的。有些库可以简化编写控制台用户界面(例如Curses),但我相信除了只显示一个精灵外,你还有更多游戏功能。

如果你想写一个游戏,我建议看看一些图形/游戏框架/库,例如SDL

答案 3 :(得分:1)

我在code :: blocks中使用了windows.h绘制了直线。我无法详细解释它,但我可以为您提供代码和过程,以便在code :: blocks中编译它。

  1. 转到设置菜单并选择编译器和调试器。
  2. 单击链接器选项卡并添加链接库libgdi32.a,该库位于C:\ Program Files \ CodeBlocks \ MinGW \ lib目录。
  3. 现在编译这个程序

    #include <windows.h>
    
    #include <cmath>
    
    #define ROUND(a) ((int) (a + 0.5))
    
    /* set window handle */
    
    static HWND sHwnd;
    
    static COLORREF redColor=RGB(255,0,0);
    
    static COLORREF blueColor=RGB(0,0,255);
    
    static COLORREF greenColor=RGB(0,255,0);
    
    
    void SetWindowHandle(HWND hwnd){
    
    sHwnd=hwnd;
    
    }
    
    /* SetPixel */
    
    void setPixel(int x,int y,COLORREF& color=redColor){
    
    if(sHwnd==NULL){
    
        MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
    
        exit(0);
    
    }
    
    HDC hdc=GetDC(sHwnd);
    
    SetPixel(hdc,x,y,color);
    
    ReleaseDC(sHwnd,hdc);
    
    return;
    
    // NEVERREACH //
    
    }
    
    
    void drawLineDDA(int xa, int ya, int xb, int yb){
    
       int dx = xb - xa, dy = yb - ya, steps, k;
    
       float xIncrement, yIncrement, x = xa, y = ya;
    
       if(abs(dx) > abs(dy)) steps = abs(dx);
    
       else steps = abs(dy);
    
       xIncrement = dx / (float) steps;
    
       yIncrement = dy / (float) steps;
    
       setPixel(ROUND(x), ROUND(y));
    
       for(int k = 0; k < steps; k++){
    
        x += xIncrement;
    
        y += yIncrement;
    
        setPixel(x, y);
    
     }
    
    }
    
    /* Window Procedure WndProc */
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
    
     switch(message){
    
        case WM_PAINT:
    
            SetWindowHandle(hwnd);
    
            drawLineDDA(10, 20, 250, 300);
    
            break;
    
        case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
    
            break;
    
        case WM_DESTROY:
    
            PostQuitMessage(0);
    
            return 0;
    
        default:
    
        break; // FAIL to call DefWindowProc //
    
      }
    
     return DefWindowProc(hwnd,message,wParam,lParam);
    
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int      iCmdShow){
    
    static TCHAR szAppName[] = TEXT("Straight Line");
    
    WNDCLASS wndclass;
    
    wndclass.style         = CS_HREDRAW|CS_VREDRAW ;
    
    wndclass.lpfnWndProc   = WndProc ;
    
    wndclass.cbClsExtra    = 0 ;
    
    wndclass.cbWndExtra    = 0 ;
    
    wndclass.hInstance     = hInstance ;
    
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    
    wndclass.lpszMenuName  = NULL ;
    
    wndclass.lpszClassName = szAppName ;
    
    // Register the window //
    
    if(!RegisterClass(&wndclass)){
    
        MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
    
        exit(0);
    
    }
    
    // CreateWindow //
    
    HWND hwnd=CreateWindow(szAppName,"DDA - Programming Techniques",
    
                WS_OVERLAPPEDWINDOW,
    
                 CW_USEDEFAULT,
    
                 CW_USEDEFAULT,
    
                 CW_USEDEFAULT,
    
                 CW_USEDEFAULT,
    
                 NULL,
    
                 NULL,
    
                 hInstance,
    
                 NULL);
    
    if(!hwnd){
    
        MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
    
        exit(0);
    
      }
    
      // ShowWindow and UpdateWindow //
    
      ShowWindow(hwnd,iCmdShow);
    
     UpdateWindow(hwnd);
    
     // Message Loop //
    
     MSG msg;
    
     while(GetMessage(&msg,NULL,0,0)){
    
        TranslateMessage(&msg);
    
        DispatchMessage(&msg);
    
     }
    
      /* return no error to the operating system */
    
      return 0;
    
    }
    

    在这个程序中,我使用了DDA线条绘制算法。像素绘制任务由setPixel(ROUND(x),ROUND(y))函数完成。 这是Windows编程,您可以了解详细信息here

答案 4 :(得分:0)

要在CodeBlocks中使用,我发现了这个(你必须添加一个链接器选项-lgdi32): //代码块:项目构建选项链接器设置Othoer链接器选项:add -lgdi32

我忘记了:你必须在包含windows.h之前把它放进去:#define _WIN32_WINNT 0x0500

再次使用整个余弦代码。准备编译

//Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32
#define _WIN32_WINNT 0x0500
#include "windows.h"
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14
int main(){
    HWND myconsole = GetConsoleWindow();
    HDC mydc = GetDC(myconsole);
    int pixel =0;
    COLORREF COLOR= RGB(255,255,255);

    //Draw pixels
    for(double i = 0; i < PI * 4; i += 0.05)
    {
        SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
        pixel+=1;
    }

    ReleaseDC(myconsole, mydc);
    cin.ignore();
    return 0;
}
相关问题