2012-09-11 133 views
11

在使用Code :: Blocks v10.05的C++中,如何在控制檯屏幕上繪製單個像素?這很簡單嗎?還是僅僅畫一個矩形會更容易?我如何爲它着色?我很抱歉,但我無法從SOF,HF甚至cplusplus.com獲取任何代碼來工作。這是在屏幕上的超級馬里奧世界的數字。我認爲這款遊戲是16位的,並且適用於SNES系統。 C :: B說我需要SDK for C :: B。它說「afxwin.h」不存在。也許下載?這就是我想要做:控制檯窗口中的C++像素

Image I'm trying to create

+0

你能鏈接到你無法工作的代碼嗎? – Borgleader

+0

哇,這需要我回來......人們過去在DOS中這樣做。你必須跳到存儲字符集的內存中並對其進行修改(在DOS中幾乎所有的常見操作,包括在其他圖形模式下將字節寫入視頻卡),然後使用修改後的字符繪製你的精靈。顯然你受限於256個可能的子圖像。我不知道這是否仍然在Windows中發生。無法給出答案,對不起,除了說控制檯窗口只是另一個窗口。我想知道你是否可以掛鉤它的WM_PAINT消息。 – paddy

+0

http://stackoverflow.com/questions/4576019/c-drawing-pixels-question – hCon

回答

1

控制檯是一個文本設備,所以一般你不寫單個像素。您可以創建一個特殊的字體並將其選爲控制檯的字體,但它會是單色的。有些庫可以簡化編寫控制檯UI(例如Curses),但是我相信除了只顯示一個精靈之外,你還會有更多類似於遊戲的功能。

如果你想寫遊戲,我建議看看一些圖形/遊戲框架/庫,例如, SDL

+0

所以我應該在窗口中顯示一個精靈/圖像?如果是這樣,我該怎麼做? – hCon

+0

你需要做什麼,例如對於學校來說,自己做所有像素修改代碼非常重要,或者您對結果更感興趣?如果是前者,你使用的是什麼環境(Win,Linux等 - 我猜Windows是基於afxwin.h)? 如果是後者,下面是一個教程:http://www.libsdl.org/intro.en/usingvideo.html –

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等。

+0

Gross:D但是,對於「解決方案」而言,+1。請注意,您不會處理'WM_PAINT',因此如果區域無效,它將被刪除。正常的窗戶強烈希望超過這個。 – tenfour

+0

代碼::塊錯誤:'GetConsoleWindow'未在此解決方案中聲明。 – hCon

+1

@Mike是否包含「windows.h」頭文件?記住你必須從微軟獲得它。您必須下載微軟SDK:http://www.microsoft.com/en-us/download/details.aspx?id=3138 – FacundoGFlores

10

如果您願意使圖像顯得塊狀,可以利用console code page中的塊字符。

  • = '\ XDB'= U + 2588 FULL BLOCK
  • = '\ XDC'= U + 2584下半部BLOCK
  • = '\ XDF'= U + 2580上半部BLOCK
  • 和空間

通過組合使用半塊與colored text,可以把一個80 × 25控制檯窗口到80 × 50 16彩色顯示。 (這是QBasic版本Nibbles使用的方法。)

然後,您只需要將圖像轉換爲16色調色板和合理的小尺寸。

Mario in 8 lines and 10 columns of "text"

1

我畫中的代碼::塊使用WINDOWS.H直線。我無法詳細解釋它,但我可以爲您提供一個代碼和過程,以便在code :: blocks中進行編譯。

  1. 轉到設置菜單並選擇編譯器和調試器。
  2. 單擊鏈接器選項卡並添加鏈接庫libgdi32.a,該鏈接庫位於C:\ Program Files \ CodeBlocks \ MinGW \ lib目錄中。

現在編譯這個程序

#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

1

windows.h提供了一個功能SetPixel()在窗口的指定位置打印像素。函數的一般形式是

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

其中,x和y是要顯示的像素的座標,color是像素的顏色。

重要:與代碼::塊打印像素機器IDE,添加一個鏈接庫libgdi32.a(通常是內部MinGW\lib)的連接設置。

0

要在代碼塊,我發現這個使用(你必須添加一個連接器選項-lgdi32): //代碼塊:項目編譯選項鍊接設置Othoer連接選項:添加-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; 
}