OSDN Git Service

e1f1a6cae2b2223c76ad42280feb73b88d85d490
[chnosproject/AI004.git] / webcpu / api.js
1 function WebCPU_API(){
2         this.mainWindowCanvas = null;
3         this.mainWindowContext = null;
4         this.mainWindowBufferCanvas = document.createElement('canvas');
5         this.mainWindowBufferContext = this.mainWindowBufferCanvas.getContext("2d");
6         //
7         this.mainWindowBufferCanvas.width = 640;
8         this.mainWindowBufferCanvas.height = 480;
9         this.initCanvas(this.mainWindowBufferCanvas);
10 }
11 WebCPU_API.prototype = {
12         executeAPI: function(env){
13                 var r = env.registers.Integer;
14                 var APIID = r[0x30];
15                 switch(APIID){
16                         case 0xff40:
17                                 //openWin
18                                 this.API_openWin(env, r[0x31], r[0x32])
19                                 break;
20                         case 0xff41:
21                                 //openWin
22                                 this.API_flushWin(env, r[0x31], r[0x32], r[0x33], r[0x34])
23                                 break;
24                         case 0xff44:
25                                 //drawPoint
26                                 this.API_drawPoint(env, r[0x31], r[0x32], r[0x33], r[0x34]);
27                                 break;
28                         case 0xff45:
29                                 //drawLine
30                                 this.API_drawLine(env, r[0x31], r[0x32], r[0x33], r[0x34], r[0x35], r[0x36]);
31                                 break;
32                         case 0xff46:
33                                 //fillRect
34                                 this.API_fillRect(env, r[0x31], r[0x32], r[0x33], r[0x34], r[0x35], r[0x36]);
35                                 break;
36                         case 0xff47:
37                                 //fillOval
38                                 this.API_fillOval(env, r[0x31], r[0x32], r[0x33], r[0x34], r[0x35], r[0x36]);
39                                 break;
40                         default:
41                                 throw new WebCPU_Exception(0, ["Unknown API Number " + APIID.toString(16)]);
42                 }
43                 env.goToPointerRegister(0x30);
44         },
45         colorTable:[
46                 0x000000, 
47                 0xff0000, 
48                 0x00ff00, 
49                 0xffff00, 
50                 0x0000ff, 
51                 0xff00ff, 
52                 0x00ffff, 
53                 0xffffff
54         ],
55         setMainWindowCanvasDOMObject: function(obj){
56                 this.mainWindowCanvas = obj;
57                 if(this.mainWindowCanvas){
58                         this.mainWindowContext = this.mainWindowCanvas.getContext('2d')
59                         this.initCanvas(this.mainWindowCanvas);
60                 } else{
61                         this.mainWindowContext = null;
62                 }
63         },
64         initCanvas: function(c){
65                 var d = c.getContext('2d');
66                 d.fillStyle = "rgba(0,0,0,1)";
67                 d.strokeStyle = "rgba(255, 255, 255, 1)";
68                 d.lineWidth = 1;
69                 //描画域は必ず黒で初期化されます。
70                 d.fillRect(0, 0, c.width, c.height);
71         },
72         //
73         API_openWin: function(env, xSize, ySize){
74                 env.message("junkApi_openWin();\n", 20);
75                 this.mainWindowCanvas.width = xSize;
76                 this.mainWindowCanvas.height = ySize;
77                 this.mainWindowBufferCanvas.width = xSize;
78                 this.mainWindowBufferCanvas.height = ySize;
79         },
80         API_flushWin: function(env, xSize, ySize, x0, y0){
81                 env.message("junkApi_flushWin();\n", 20);
82                 this.mainWindowContext.drawImage(this.mainWindowBufferCanvas, x0, y0, xSize, ySize, 0, 0, xSize, ySize);
83         },
84         API_drawPoint: function(env, mode, x, y, col){
85                 env.message("junkApi_drawPoint();\n", 20);
86                 if((mode & 0x04) != 0){
87                         col = this.colorTable[col];
88                 }
89                 this.mainWindowBufferContext.fillStyle = "#" + ("000000" + col.toString(16)).slice(-6).toUpperCase();
90                 this.mainWindowBufferContext.fillRect(x, y, 1, 1);
91         },
92         API_drawLine: function(env, mode, x0, y0, x1, y1, col){
93                 env.message("junkApi_drawLine();\n", 20);
94
95                 if((mode & 0x04) != 0){
96                         col = this.colorTable[col];
97                 }
98                 
99                 this.mainWindowBufferContext.strokeStyle = "#" + ("000000" + col.toString(16)).slice(-6).toUpperCase();
100                 this.mainWindowBufferContext.beginPath();
101                 this.mainWindowBufferContext.moveTo(x0, y0);
102                 this.mainWindowBufferContext.lineTo(x1, y1);
103                 this.mainWindowBufferContext.closePath();
104                 this.mainWindowBufferContext.stroke();
105         },
106         API_fillRect: function(env, mode, xSize, ySize, x0, y0, col){
107                 env.message("junkApi_fillRect();\n", 20);
108
109                 if((mode & 0x04) != 0){
110                         col = this.colorTable[col];
111                 }
112                 
113                 this.mainWindowBufferContext.fillStyle = "#" + ("000000" + col.toString(16)).slice(-6).toUpperCase();
114                 this.mainWindowBufferContext.fillRect(x0, y0, xSize, ySize);
115         },
116         API_fillOval: function(env, mode, xSize, ySize, x0, y0, col){
117                 env.message("junkApi_fillRect();\n", 20);
118
119                 if((mode & 0x04) != 0){
120                         col = this.colorTable[col];
121                 }
122                 
123                 this.mainWindowBufferContext.fillStyle = "#" + ("000000" + col.toString(16)).slice(-6).toUpperCase();
124                 this.mainWindowBufferContext.fillEllipse(x0, y0, xSize, ySize);
125         },
126 }