News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Color in c++ console applciation using asm

Started by ReVeR, February 26, 2005, 12:11:25 AM

Previous topic - Next topic

ReVeR

Hello.
i am trying to make my c++ application have some nice colors, etc....and i want to be able to use the 32 bit color scheme. Looking over some stuff i think that the answer is in asm.....since c++ doesn't support anything close to this. So i was wondering if anyone here knows anything related to this, that might help me out?
Thx in advance

OceanJeff32

What code have you got so far?  If you want a 32-bit color pixel, you use one byte for alpha, and one byte for each red, green, blue, to make any 32-bit color you desire.

You could use inline assembly to make the 32-bit color and then store that in the c++ variable.

Later,

Jeff C
:8)
Any good programmer knows, every large and/or small job, is equally large, to the programmer!

ReVeR

is ther an example piece of code i can try?or something like that...becusae for it to work , i need to set the 32 bit mode somehow, and then i can only use the 3 values for the bytes.

exluddite

I did a quick scan at this site:
http://www.koders.com/
and found some things that might be helpful.
By the way, I think this is a great resource for whatever you're working on.

MichaelW

AFAIK, at least by default, the console uses normal 16-color text mode attributes.

MSDN: Console Reference


; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                       ; create 32 bit code
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc

    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\kernel32.lib

    include \masm32\macros\macros.asm

    SetTextColor PROTO :DWORD,:DWORD

    SetScreenColor PROTO :DWORD,:DWORD

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    invoke locate,0,2
    mov   ebx,32
    mov   edi,0
  @@:
    invoke SetTextColor,0,edi
    print chr$("XX")
    inc   edi
    dec   ebx
    jnz   @B

    mov   eax,input(13,10,13,10,"Press enter to continue...")

    invoke SetScreenColor,15,1

    mov   eax,input(13,10,"Press enter to exit...")
    exit

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; This proc sets the foreground and background attributes for
; the characters written to the console screen buffer.
;
; The color values are normal 4-bit IRGB character mode
; attributes where:
;   bit3 = intensity
;   bit2 = red
;   bit1 = green
;   bit0 = blue
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
SetTextColor proc fore:DWORD,back:DWORD

    LOCAL hStdOut:DWORD

    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov   hStdOut,eax

    ; The attribute is stored in a single byte.
    mov   eax,back
    shl   eax,4
    or    eax,fore
    invoke SetConsoleTextAttribute,hStdOut,eax

    ret
SetTextColor endp

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; This is the MASM32 ClearScreen procedure modified to set the
; attribute bytes for the entire screen buffer to the value
; specified by the arguments.
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
SetScreenColor proc fore:DWORD,back:DWORD

    LOCAL hOutPut:DWORD
    LOCAL noc    :DWORD
    LOCAL cnt    :DWORD
    LOCAL sbi    :CONSOLE_SCREEN_BUFFER_INFO

    ;cls

    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov hOutPut, eax

    invoke GetConsoleScreenBufferInfo,hOutPut,ADDR sbi

    mov eax, sbi.dwSize     ; 2 word values returned for screen size

  ; -----------------------------------------------
  ; extract the 2 values and multiply them together
  ; -----------------------------------------------
    push ax
    rol eax, 16
    mov cx, ax
    pop ax
    mul cx
    cwde
    mov cnt, eax

    ; The attribute is stored in a single byte.
    mov   edx,back
    shl   edx,4
    or    edx,fore

    invoke FillConsoleOutputAttribute,hOutPut,edx,cnt,NULL,ADDR noc

    ret

SetScreenColor endp

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

Vortex


ReVeR

hey, thx for your replays all. I checked out the koder web site....the codefor color doens't work for windows in most cases, or is not for it at all. Some examples need their own kernel, ohter are a part of large libs focust onto linux.
ex, openPTC.
Thx for the above code, but again, it only gives me access to liek 16 colors.....
how can i set a 32 bit color mode in a console? and how do i use it?
i had the code somewhere from some example but i lost it now.....