From 1563aa41b69c9cfaf35869bd7a58500deb2214e1 Mon Sep 17 00:00:00 2001 From: hinus Date: Sun, 19 Jul 2020 23:10:13 +0800 Subject: [PATCH] =?UTF-8?q?Summary:=20=E5=88=9D=E6=AD=A5=E5=85=B7=E5=A4=87?= =?UTF-8?q?=E7=BB=98=E5=88=B6=E5=8A=A8=E7=94=BB=E7=9A=84=E8=83=BD=E5=8A=9B?= =?UTF-8?q?=20Issue:=20https://gitee.com/hinus/HiLang/issues/I1OBBZ=20Desc?= =?UTF-8?q?ription:=201.=20Linux=20=E4=B8=8A=E7=9A=84opengl=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E9=85=8D=E7=BD=AE=E5=B7=B2=E7=BB=8F=E5=9C=A8issue?= =?UTF-8?q?=E9=87=8C=E8=AF=B4=E6=98=8E=E3=80=82=202.=20windows=20=E4=B8=8A?= =?UTF-8?q?=E7=9A=84=E7=BB=98=E5=88=B6=E5=9F=BA=E6=9C=AC=E6=88=90=E5=BD=A2?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=E7=9B=AE=E5=89=8D=E5=8F=AA=E8=83=BD?= =?UTF-8?q?=E6=94=AF=E6=8C=81opengl1.5=E4=BB=A5=E4=B8=8B=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E3=80=82=20LLT:=20win.hi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/vm/extlib/hidraw.cpp | 51 ++++++++++++++++++++++++--- src/main/vm/extlib/hidraw.hpp | 4 +++ src/main/vm/os/windows/hidraw_win.cpp | 15 +++++--- src/test/hilang/win.hi | 17 ++++++--- 4 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/main/vm/extlib/hidraw.cpp b/src/main/vm/extlib/hidraw.cpp index b89b79c..eba2cea 100644 --- a/src/main/vm/extlib/hidraw.cpp +++ b/src/main/vm/extlib/hidraw.cpp @@ -1,8 +1,10 @@ #include "inc/railgun.hpp" #include "extlib/hidraw.hpp" -#include -#include +#include +#include + +#include #ifdef RAILGUN_WINDOWS #include @@ -27,6 +29,7 @@ HiObject* window_get_events(ObjList args) { DispatchMessage(&msg); } else { + printf("peek %d messages.\n", i); return events; } } @@ -46,11 +49,21 @@ double get_double(HiTuple* args, int index) { return y; } +HiObject* canvas_fill(ObjList args) { + HiTuple* color = (HiTuple*)args->get(1); + double red = get_double(color, 0) / 255.0; + double green = get_double(color, 1) / 255.0; + double blue = get_double(color, 2) / 255.0; + + glClearColor(red, green, blue, 0.0); + glClear(GL_COLOR_BUFFER_BIT); + + return Universe::HiNone; +} HiObject* canvas_draw_line(ObjList args) { HiTuple* p1 = (HiTuple*)args->get(1); HiTuple* p2 = (HiTuple*)args->get(2); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // draw a line glLineWidth(1.0f); //line width @@ -59,8 +72,36 @@ HiObject* canvas_draw_line(ObjList args) { glVertex2f(get_double(p2, 0), get_double(p2, 1)); glEnd(); - glFlush(); + return Universe::HiNone; +} + +HiObject* canvas_draw_circle(ObjList args) { + const int steps = 100; + HiTuple* point = (HiTuple*)args->get(1); + double radius = ((HiDouble*)args->get(2))->value(); + HiTuple* color = (HiTuple*)args->get(3); + + double red = get_double(color, 0) / 255.0; + double green = get_double(color, 1) / 255.0; + double blue = get_double(color, 2) / 255.0; + + double x = get_double(point, 0); + double y = get_double(point, 1); + + glColor3f(red, green, blue); + + glBegin(GL_POLYGON); + for(int i=0; i < steps; i++) { + double angle = 2 * 3.14159265359 / steps * i; + glVertex2f(radius * cos(angle) + x, + radius * sin(angle) + y); + } + glEnd(); + + return Universe::HiNone; +} +HiObject* canvas_draw_test(ObjList) { return Universe::HiNone; } @@ -93,7 +134,9 @@ SO_PUBLIC ModuleObject* init_libdraw() { HiDict* klass_dict = canvas_klass->klass_dict(); klass_dict->put(new HiString("draw_line"), new FunctionObject(canvas_draw_line)); + klass_dict->put(new HiString("draw_circle"), new FunctionObject(canvas_draw_circle)); klass_dict->put(new HiString("update"), new FunctionObject(canvas_update)); + klass_dict->put(new HiString("fill"), new FunctionObject(canvas_fill)); HiObject* tp_win = draw_def->getattr(new HiString("Window")); Klass* win_klass = ((HiTypeObject*)tp_win)->own_klass(); diff --git a/src/main/vm/extlib/hidraw.hpp b/src/main/vm/extlib/hidraw.hpp index b6f34d7..23f33c8 100644 --- a/src/main/vm/extlib/hidraw.hpp +++ b/src/main/vm/extlib/hidraw.hpp @@ -13,4 +13,8 @@ HiObject* create_window_md(ObjList args); // methods definiations for canvas. HiObject* canvas_draw_line(ObjList args); HiObject* canvas_update(ObjList args); +HiObject* canvas_fill(ObjList args); + +// test case +HiObject* canvas_draw_triangle(ObjList args); #endif \ No newline at end of file diff --git a/src/main/vm/os/windows/hidraw_win.cpp b/src/main/vm/os/windows/hidraw_win.cpp index 480d869..92ad30b 100644 --- a/src/main/vm/os/windows/hidraw_win.cpp +++ b/src/main/vm/os/windows/hidraw_win.cpp @@ -37,9 +37,11 @@ void SetupPixelFomat(HWND hWnd, HDC &hDC ) { SetPixelFormat(hDC, pixelFormat, &pfd); } -bool InitGL(HDC hDC, HGLRC &hRC ) { +bool InitGL(HDC hDC, HGLRC &hRC, int width, int height) { hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); + + glViewport(0, 0, width, height); return true; } @@ -58,7 +60,10 @@ void SceneInit() } HiObject* create_window_md(ObjList args) { - MSG msg; + HiTuple* win_size = (HiTuple*)args->get(0); + HiInteger* width = (HiInteger*)win_size->get(0); + HiInteger* height = (HiInteger*)win_size->get(1); + HWND hwnd; WNDCLASS wndclass; TCHAR szAppName[] = TEXT("HiDraw"); @@ -84,8 +89,8 @@ HiObject* create_window_md(ObjList args) { WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MINIMIZEBOX ^ WS_MAXIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, + width->value(), + height->value(), NULL,NULL, NULL, NULL); @@ -94,7 +99,7 @@ HiObject* create_window_md(ObjList args) { HDC g_hDC; SetupPixelFomat(hwnd, g_hDC); - InitGL(g_hDC, g_hRC); + InitGL(g_hDC, g_hRC, width->value(), height->value()); ShowWindow(hwnd,SW_NORMAL); UpdateWindow(hwnd); diff --git a/src/test/hilang/win.hi b/src/test/hilang/win.hi index 89e50df..1758c07 100644 --- a/src/test/hilang/win.hi +++ b/src/test/hilang/win.hi @@ -1,20 +1,27 @@ from libdraw import create_window; from libdraw import Window; +import math; -myWindow = Window(create_window()); +myWindow = Window(create_window((600, 600))); print(myWindow); canvas = myWindow.get_canvas(); print(canvas); going = True; -y = -0.9; +alpha = 0.0; + while(going) { - sleep(200); + sleep(20); + + x = 0.5 * math.cos(alpha); + y = 0.5 * math.sin(alpha); - canvas.draw_line((-0.5, y), (0.5, y)); - y += 0.05; + canvas.fill((64, 64, 64)); + canvas.draw_circle((x, y), 0.1, (220, 128, 128)); canvas.update(); + alpha += 0.01; + evts = myWindow.get_events(); if (evts is None) { -- Gitee