PLOT and DRAW with APi

Started by Zlatko Vid, December 21, 2022, 07:48:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid

Hello Charles & ALL

I am trying to make classic PLOT and DRAW functions using api calls

MoveToEx hdc, x,y ,lppt

and

LineTo hdc,x1,y1

and i made search all over net but nothing concrete
i just found this small example in C ...so
how this can be translated to Oxygenbasic...
any hint?

static POINT ptPrevious = { 0,0 };
static bool flag = false;
Vertex temp;
...
case WM_LBUTTONDOWN:
        HDC hdc = GetDC(hWnd);
        POINTS clickPoint = MAKEPOINTS(lParam);
        if (flag == false) {
            ptPrevious.x = clickPoint.x;
            ptPrevious.y = clickPoint.y;
            flag = true;
        }
        //store the point in connectLines
        temp.SetX(clickPoint.x);
        temp.SetY(clickPoint.y);
        connectLines.push_back(temp);

        MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
        LineTo(hdc, LOWORD(lParam), HIWORD(lParam));

        //record previous point
        ptPrevious.x = clickPoint.x;
        ptPrevious.y = clickPoint.y;

        ReleaseDC(hWnd, hdc);
        break;
  •  

Charles Pegge

#1
Hi Aurel,

uses corewin includes all the GDI functions. Then it's much the same as your C snippet.  But it is better to store all the plots into an array, so you can do screen refreshes, and save/reload the plot.

Zlatko Vid

#2
Hi Charles

I agree that would be nice to store data in array
  •  

Theo Gottwald

if you like speed, make these commands in ASM.
You so know know ASM?
Ask ChatGPT he can give you the ASM Code for all these commands.
I have tried, it works. Ok, it may need small corrections here and there  but generally its fine.


section .data

; Define the width and height of the image
width   db 100
height  db 100

; Define the starting x and y position of the line
x_start db 50
y_start db 50

; Define the angle and length of the line
angle   dd 3.14159265358979 / 4
length  dd 50

section .bss

; Reserve space for the image buffer
image_buffer resb width * height * 3

section .text

; The main function
global _start

_start:
    ; Load the angle and length into the x87 FPU
    fld angle
    fsin
    fstp [sin_result]
    fld angle
    fcos
    fstp [cos_result]

    ; Calculate the end position of the line
    mov eax, [length]
    fild eax
    fmul dword [sin_result]
    fistp [y_end]
    mov eax, [length]
    fild eax
    fmul dword [cos_result]
    fistp [x_end]

    ; Add the starting position to the end position to get the final end position
    add [x_end], [x_start]
    add [y_end], [y_start]

    ; Calculate the delta x and y
    mov eax, [x_end]
    sub eax, [x_start]
    mov [delta_x], eax
    mov eax, [y_end]
    sub eax, [y_start]
    mov [delta_y], eax

    ; Calculate the address of the starting pixel
    mov ebx, image_buffer
    add ebx, y_start * width * 3
    add ebx, x_start * 3

    ; Draw the line
    mov eax, [delta_x]
    mov edx, [delta_y]
    cmp eax, 0
    jge .x_positive
    neg eax
    .x_positive:
    cmp edx, 0
    jge


Zlatko Vid

Quoteif you like speed, make these commands in ASM

Oh ..you don't know ?
o2 basic and o2 asm run at same speed ..i TESTED
  •  

Theo Gottwald

But in O2 you still need to handcode it yourself :-)

Nicola

Hi,
it might be easier to use WINDOW.INC under !PROJB?

Nicola

Hi,
one question, using WINDOW.INC how do I intercept a single press of the mouse key and intercept its release?
If MouseButton=1 this intercepts my left mouse button... how do I know when I've released it?
Should I loop to see when the MouseButton returns to zero?
I tried it but it crashes the program.

Cheers

Charles Pegge

#8
Hi Nicola,

Make sure you have a Sleep call inside loops, so that WndProc messages get serviced.

This is the wndproc in window.inc. You can see how it sets MouseButton and other state variables.
Function WndProc(byval hWnd as sys,byval wMsg as sys, byval wParam as sys,byval lparam as sys) as sys callback
iF wMsg = WM_CREATE
'
ElseiF wMsg = WM_PAINT
'
ElseiF wMsg = WM_SIZE
FlipBuffer
'
ElseiF wMsg = WM_MOVE
FlipBuffer
ValidateRect sys_hwnd,0
'
ElseiF wMsg = WM_MOUSEMOVE
xMouse = LoWord(lParam)
yMouse = HiWord(lParam)
InValidateRect sys_hwnd,0,0
'
ElseiF wMsg = WM_LBUTTONDOWN
MouseButton =1
InvalidateRect hWnd,0,0
ElseiF wMsg = WM_LBUTTONUP
MouseButton =0 
InvalidateRect hWnd,0,0
ElseiF wMsg = WM_RBUTTONDOWN
MouseButton =2
InvalidateRect hWnd,0,0
ElseiF wMsg = WM_RBUTTONUP
MouseButton =0
InvalidateRect hWnd,0,0
ElseiF wMsg = WM_MBUTTONDOWN
MouseButton =4
InvalidateRect hWnd,0,0
ElseiF wMsg = WM_MBUTTONUP
MouseButton =0
InvalidateRect hWnd,0,0
'
ElseiF wMsg = WM_CHAR
AscKey = wParam
InvalidateRect hWnd,0,0
'
ElseiF wMsg = WM_KEYUP
AscKey =0
InvalidateRect hWnd,0,0
'
ElseiF wMsg = WM_DESTROY
PostQuitMessage 0
Else
Function = DefWindowProc hWnd,wMsg,wParam,lParam
End iF
End Function

Nicola

Charles,
why doesn't it work properly?


'Paintn
'--------------
indexBase 0
include "window.inc"

redim int pu[20,2] 'contiene le coordinate dei punti
int pp = 0 'puntatore di pu[]

Sub WaitFrames(sys Frame)   
    QueryPerformanceFrequency sys_Freq                                                                                         
    sys_Interval = sys_Freq/Frame 
    While (1)                                                                     
        QueryPerformanceCounter sys_cT2                                               
        iF sys_cT2 >= sys_cT1 + sys_Interval                                           
            QueryPerformanceCounter sys_cT1                                             
            Exit Sub           
        End iF
    Wend                                                                           
End Sub

Sub ReleaseMouse()
    while 1
        sleep 1000
        if mousebutton=0 then exit while
    wend
end sub

Window "Paint",640,480,1
single vel, xOff, yOff, a, angle
int r,g,b,rc,gc,bc, j
rc=0xc8
gc=0xc8
bc=0xf8
r=255
g=0
b=0

clsColor rc,gc,bc
While WinExit=0
 if key(82)<>0 then r=r+1 : r and=255
 if key(71)<>0 then g=g+1 : g and=255
 if key(66)<>0 then b=b+1 : b and=255

 iF MouseButton=1
    pp = pp + 1
    pu[pp,0] = xMouse
    pu[pp,1] = yMouse
    sleep(200)
    'ReleaseMouse()
    if pp>1 then
        line (pu[pp,0], pu[pp,1], pu[pp-1,0], pu[pp-1,1],2,r,g,b)
    end if
  End iF

  iF MouseButton=2 Then
    clsColor rc,gc,bc
  text 10,20,"cls",r,g,b
    sleep(50)
    int j
 
    text 1,1,"redraw fatto",r,g,b
    pp=0
    sleep 100
  clsColor rc,gc,bc
    for j=2 to pp
        line (pu[j,0], pu[j,1], pu[j-1,0], pu[j-1,1],2,r,g,b)
    next
  end if
 
  Events
  FlipBuffer
  WaitFrames 34
Wend
WinEnd

Charles Pegge

I rarely use Peter Wirbelauer's API but I would start with one of his GDI examples and make incremental changes, working towards the program you want to achieve. Bugs are detected and easily isolated during this process. It's how nature does it, and also AI :)

Nicola

Hi Charles,
I'm sorry, I thought you knew about it. Anyway I did as you said. In fact, the basis of the program was already there. I just entered a few commands. Furthermore, I believe that its operation is very elementary to be able to create problems, unless the API you were talking about does not have a non-linear management of the commands.

Charles Pegge

This API is really intended for 2D games and single-window graphics demos. Nonetheless it has some very useful 'seed' functions. I think what we need is a system that does the graphics but also supports child windows for controlling and script editing. So I had the idea that we adapt an IDE and create an additional space for rendering graphics onto the main window...

Zlatko Vid

hello
Additional space in IDE...then that looks like BASIC256 ..he he
  •  

Zlatko Vid

I must say that i lerned programming in o2 with Peter include and examples ..
and yes Charles have right Peter WINDOW.inc is creted for 2D programming
and is not bad at all,it have some quirks but in general work fine and is simple .
On the other side my awinho37.inc ..is about building GUI programs and also have some quirks
but also ...as far i use it work well.
  •