1 /***************************************************************************
5 Created by Gil Pedersen on Sun April 18 2004.
6 Copyright (c) 2004 Gil Pedersen.
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. See also the license.txt file for *
15 * additional informations. *
17 ***************************************************************************/
20 #import <OpenGL/glext.h>
21 #import <OpenGL/glu.h>
23 #import <Carbon/Carbon.h>
24 #import "PluginGLView.h"
25 #include "externals.h"
33 static int mylog2(int val)
44 void BlitScreen16NS(unsigned char * surf,long x,long y)
47 unsigned short row,column;
48 unsigned short dx=PreviousPSXDisplay.Range.x1>>1;
49 unsigned short dy=PreviousPSXDisplay.DisplayMode.y;
50 unsigned short LineOffset,SurfOffset;
51 long lPitch=image_width<<1;
53 if(PreviousPSXDisplay.Range.y0) // centering needed?
55 surf+=PreviousPSXDisplay.Range.y0*lPitch;
56 dy-=PreviousPSXDisplay.Range.y0;
60 unsigned long * SRCPtr = (unsigned long *)(psxVuw + (y<<10) + x);
61 unsigned long * DSTPtr = ((unsigned long *)surf)+(PreviousPSXDisplay.Range.x0>>1);
63 LineOffset = 512 - dx;
64 SurfOffset = (lPitch>>2) - dx;
66 for(column=0;column<dy;column++)
68 for(row=0;row<dx;row++)
70 lu=GETLE16D(SRCPtr++);
72 *DSTPtr++= lu;//((lu<<11)&0xf800f800)|((lu<<1)&0x7c007c0)|((lu>>10)&0x1f001f);
81 @implementation PluginGLView
83 //- (id)initWithFrame:(NSRect)frameRect
84 - (id) initWithCoder: (NSCoder *) coder
86 const GLubyte * strExt;
88 if ((self = [super initWithCoder:coder]) == nil)
91 glLock = [[NSLock alloc] init];
97 // Init pixel format attribs
98 NSOpenGLPixelFormatAttribute attrs[] =
100 NSOpenGLPFAAccelerated,
101 NSOpenGLPFANoRecovery,
102 NSOpenGLPFADoubleBuffer,
106 // Get pixel format from OpenGL
107 NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
110 NSLog(@"No Accelerated OpenGL pixel format found\n");
112 NSOpenGLPixelFormatAttribute attrs2[] =
114 NSOpenGLPFANoRecovery,
118 // Get pixel format from OpenGL
119 pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs2];
121 NSLog(@"No OpenGL pixel format found!\n");
128 [self setPixelFormat:[pixFmt autorelease]];
131 long swapInterval = 1 ;
132 [[self openGLContext]
133 setValues:&swapInterval
134 forParameter:NSOpenGLCPSwapInterval];
137 [[self openGLContext] makeCurrentContext];
139 // Init object members
140 strExt = glGetString (GL_EXTENSIONS);
141 texture_range = gluCheckExtension ((const unsigned char *)"GL_APPLE_texture_range", strExt) ? GL_TRUE : GL_FALSE;
142 texture_hint = GL_STORAGE_SHARED_APPLE ;
143 client_storage = gluCheckExtension ((const unsigned char *)"GL_APPLE_client_storage", strExt) ? GL_TRUE : GL_FALSE;
144 rect_texture = gluCheckExtension((const unsigned char *)"GL_EXT_texture_rectangle", strExt) ? GL_TRUE : GL_FALSE;
146 // Setup some basic OpenGL stuff
147 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
148 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
149 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
150 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
151 glClear(GL_COLOR_BUFFER_BIT);
153 [NSOpenGLContext clearCurrentContext];
160 image_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
161 image_base = (GLubyte *) calloc(((IMAGE_COUNT * image_width * image_height) / 3) * 4, image_depth >> 3);
162 if (image_base == nil) {
167 // Create and load textures for the first time
168 [self loadTextures:GL_TRUE];
171 //gettimeofday(&cycle_time, NULL);
175 // Call for a redisplay
177 PSXDisplay.Disabled = 1;
178 [self setNeedsDisplay:true];
189 [[self openGLContext] makeCurrentContext];
190 for(i = 0; i < IMAGE_COUNT; i++)
193 glDeleteTextures(1, &dt);
195 if(texture_range) glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_EXT, IMAGE_COUNT * image_width * image_height * (image_depth >> 3), image_base);
197 [NSOpenGLContext clearCurrentContext];
212 - (BOOL)acceptsFirstResponder
217 - (void)drawRect:(NSRect)aRect
219 // Check if an update has occured to the buffer
220 if ([self lockFocusIfCanDraw]) {
222 // Make this context current
224 [[NSColor blackColor] setFill];
225 [NSBezierPath fillRect:[self visibleRect]];
229 // Swap buffer to screen
230 //[[self openGLContext] flushBuffer];
237 - (void)update // moved or resized
243 [[self openGLContext] makeCurrentContext];
244 [[self openGLContext] update];
246 rect = [self bounds];
248 glViewport(0, 0, (int) rect.size.width, (int) rect.size.height);
250 glMatrixMode(GL_PROJECTION);
253 glMatrixMode(GL_MODELVIEW);
256 //[self setNeedsDisplay:true];
260 - (void)reshape // scrolled, moved or resized
264 NSOpenGLContext *oglContext = [self openGLContext];
269 [oglContext makeCurrentContext];
272 rect = [[oglContext view] bounds];
274 glViewport(0, 0, (int) rect.size.width, (int) rect.size.height);
276 glMatrixMode(GL_PROJECTION);
279 glMatrixMode(GL_MODELVIEW);
284 [NSOpenGLContext clearCurrentContext];
286 // [self setNeedsDisplay:true];
294 int bufferIndex = whichImage;
296 if (1/*[glLock tryLock]*/) {
297 // Make this context current
298 [[self openGLContext] makeCurrentContext];
299 if (PSXDisplay.Disabled) {
300 glClear(GL_COLOR_BUFFER_BIT);
302 // Bind, update and draw new image
305 glBindTexture(GL_TEXTURE_RECTANGLE_EXT, bufferIndex+1);
307 glTexSubImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0, image_width, image_height, GL_BGRA, image_type, image[bufferIndex]);
309 glTexCoord2f(0.0f, 0.0f);
310 glVertex2f(-1.0f, 1.0f);
312 glTexCoord2f(0.0f, image_height);
313 glVertex2f(-1.0f, -1.0f);
315 glTexCoord2f(image_width, image_height);
316 glVertex2f(1.0f, -1.0f);
318 glTexCoord2f(image_width, 0.0f);
319 glVertex2f(1.0f, 1.0f);
324 glBindTexture(GL_TEXTURE_2D, whichImage+1);
326 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width2, image_height2, GL_BGRA, image_type, image[bufferIndex]);
328 glTexCoord2f(0.0f, 0.0f);
329 glVertex2f(-1.0f, 1.0f);
331 glTexCoord2f(0.0f, image_ty);
332 glVertex2f(-1.0f, -1.0f);
334 glTexCoord2f(image_tx, image_ty);
335 glVertex2f(1.0f, -1.0f);
337 glTexCoord2f(image_tx, 0.0f);
338 glVertex2f(1.0f, 1.0f);
344 if(ulKeybits&KEY_SHOWFPS)
347 if(szDebugText[0] && ((time(NULL) - tStart) < 2))
349 strncpy(szDispBuf, szDebugText, 63);
355 strncat(szDispBuf, szMenuBuf, 63 - strlen(szDispBuf));
359 NSRect rect = [[[self openGLContext] view] bounds];
360 len = (int) strlen(szDispBuf);
362 glMatrixMode(GL_PROJECTION);
365 gluOrtho2D(0.0, rect.size.width, 0.0, rect.size.height);
366 glDisable(rect_texture ? GL_TEXTURE_RECTANGLE_EXT : GL_TEXTURE_2D);
368 glColor4f(0.0, 0.0, 0.0, 0.5);
369 glRasterPos2f(3.0, rect.size.height - 14.0);
370 for (i = 0; i < len; i++) {
371 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, szDispBuf[i]);
374 glColor3f(1.0, 1.0, 1.0);
375 glRasterPos2f(2.0, rect.size.height - 13.0);
376 for (i = 0; i < len; i++) {
377 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, szDispBuf[i]);
381 glEnable(rect_texture ? GL_TEXTURE_RECTANGLE_EXT : GL_TEXTURE_2D);
385 [[self openGLContext] flushBuffer];
386 [NSOpenGLContext clearCurrentContext];
391 - (void)loadTextures:(GLboolean)first
396 [[self openGLContext] makeCurrentContext];
399 printf("Range.x0=%i\n"
403 PreviousPSXDisplay.Range.x0,
404 PreviousPSXDisplay.Range.x1,
405 PreviousPSXDisplay.Range.y0,
406 PreviousPSXDisplay.Range.y1);
408 printf("DisplayMode.x=%d\n"
409 "DisplayMode.y=%d\n",
410 PreviousPSXDisplay.DisplayMode.x,
411 PreviousPSXDisplay.DisplayMode.y);
413 printf("DisplayPosition.x=%i\n"
414 "DisplayPosition.y=%i\n",
415 PreviousPSXDisplay.DisplayPosition.x,
416 PreviousPSXDisplay.DisplayPosition.y);
418 printf("DisplayEnd.x=%i\n"
420 PreviousPSXDisplay.DisplayEnd.x,
421 PreviousPSXDisplay.DisplayEnd.y);
425 PreviousPSXDisplay.Double,
426 PreviousPSXDisplay.Height);
428 printf("Disabled=%i\n", PreviousPSXDisplay.Disabled);
431 image_width = PreviousPSXDisplay.Range.x1;
432 image_height = PreviousPSXDisplay.DisplayMode.y;
433 if (PSXDisplay.RGB24) {
435 image_type = GL_UNSIGNED_INT_8_8_8_8_REV;
438 image_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
442 if (image_width * image_height * (image_depth >> 3) > ((1024*512*2)/3)*4)
443 printf("Fatal error: desired dimension are too large! (%ix%i %ibpp)\n",
444 image_width, image_height, image_depth);
446 for(i = 0; i < IMAGE_COUNT; i++)
447 image[i] = image_base + i * image_width * image_height * (image_depth >> 3);
451 image_width2 = image_width;
452 image_height2 = image_height;
453 image_tx = (float)image_width;
454 image_ty = (float)image_height;
456 if(texture_range) glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_EXT, IMAGE_COUNT * image_width * image_height * (image_depth >> 3), image_base);
457 else glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_EXT, 0, NULL);
459 for(i = 0; i < IMAGE_COUNT; i++)
464 glDeleteTextures(1, &dt);
467 glDisable(GL_TEXTURE_2D);
468 glEnable(GL_TEXTURE_RECTANGLE_EXT);
469 glBindTexture(GL_TEXTURE_RECTANGLE_EXT, i+1);
471 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_STORAGE_HINT_APPLE , texture_hint);
472 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, client_storage);
473 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
474 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
475 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
476 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
477 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
479 glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, image_width,
480 image_height, 0, GL_BGRA, image_type, image[i]);
485 image_width2 = mylog2(image_width);
486 image_height2 = mylog2(image_height);
487 image_tx = (float)image_width/(float)image_width2;
488 image_ty = (float)image_height/(float)image_height2;
490 glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_EXT, 0, NULL);
491 if(texture_range) glTextureRangeAPPLE(GL_TEXTURE_2D, IMAGE_COUNT * image_width2 * image_height2 * (image_depth >> 3), image_base);
492 else glTextureRangeAPPLE(GL_TEXTURE_2D, 0, NULL);
494 for(i = 0; i < IMAGE_COUNT; i++)
499 glDeleteTextures(1, &dt);
502 glDisable(GL_TEXTURE_RECTANGLE_EXT);
503 glEnable(GL_TEXTURE_2D);
504 glBindTexture(GL_TEXTURE_2D, i+1);
506 //if(texture_range) glTextureRangeAPPLE(GL_TEXTURE_2D, IMAGE_COUNT * image_width2 * image_height2 * (image_depth >> 3), image_base);
507 //else glTextureRangeAPPLE(GL_TEXTURE_2D, 0, NULL);
509 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_STORAGE_HINT_APPLE , texture_hint);
510 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, client_storage);
511 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
512 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
513 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
514 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
515 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
517 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width2,
518 image_height2, 0, GL_BGRA, image_type, image[i]);
522 [NSOpenGLContext clearCurrentContext];
528 unsigned char * surf;
529 long x = PSXDisplay.DisplayPosition.x;
530 long y = PSXDisplay.DisplayPosition.y;
532 unsigned short row,column;
533 unsigned short dx=(unsigned short)PSXDisplay.DisplayEnd.x;//PreviousPSXDisplay.Range.x1;
534 unsigned short dy=(unsigned short)PSXDisplay.DisplayEnd.y;//PreviousPSXDisplay.DisplayMode.y;
537 //printf("y=%i",PSXDisplay.DisplayPosition.y);
539 if ([glLock tryLock]) {
540 // make sure the texture area is ready to be written to
541 glFinishObjectAPPLE(GL_TEXTURE, 2-whichImage);
543 if ((image_width != PreviousPSXDisplay.Range.x1) ||
544 (image_height != PreviousPSXDisplay.DisplayMode.y) ||
545 ((PSXDisplay.RGB24 ? 32 : 16) != image_depth)) {
546 [self loadTextures:NO];
549 surf = image[1-whichImage];
550 lPitch=image_width2<<(image_depth >> 4);
552 if(PreviousPSXDisplay.Range.y0) // centering needed?
554 surf+=PreviousPSXDisplay.Range.y0*lPitch;
555 dy-=PreviousPSXDisplay.Range.y0;
560 unsigned char * pD;unsigned int startxy;
562 surf+=PreviousPSXDisplay.Range.x0<<2;
564 for(column=0;column<dy;column++)
566 startxy = (1024 * (column + y)) + x;
567 pD = (unsigned char *)&psxVuw[startxy];
570 // make sure the reads are aligned
571 while ((int)pD & 0x3) {
572 *((unsigned long *)((surf)+(column*lPitch)+(row<<2))) =
573 (*(pD+0)<<16)|(*(pD+1)<<8)|*(pD+2);
581 unsigned long lu1 = *((unsigned long *)pD);
582 unsigned long lu2 = *((unsigned long *)pD+1);
583 unsigned long lu3 = *((unsigned long *)pD+2);
584 unsigned long *dst = ((unsigned long *)((surf)+(column*lPitch)+(row<<2)));
587 (((lu1>>24)&0xff)<<16)|(((lu1>>16)&0xff)<<8)|(((lu1>>8)&0xff));
589 (((lu1>>0)&0xff)<<16)|(((lu2>>24)&0xff)<<8)|(((lu2>>16)&0xff));
591 (((lu2>>8)&0xff)<<16)|(((lu2>>0)&0xff)<<8)|(((lu3>>24)&0xff));
593 (((lu3>>16)&0xff)<<16)|(((lu3>>8)&0xff)<<8)|(((lu3>>0)&0xff));
596 (((lu1>>0)&0xff)<<16)|(((lu1>>8)&0xff)<<8)|(((lu1>>16)&0xff));
598 (((lu1>>24)&0xff)<<16)|(((lu2>>0)&0xff)<<8)|(((lu2>>8)&0xff));
600 (((lu2>>16)&0xff)<<16)|(((lu2>>24)&0xff)<<8)|(((lu3>>0)&0xff));
602 (((lu3>>8)&0xff)<<16)|(((lu3>>16)&0xff)<<8)|(((lu3>>24)&0xff));
607 //for(;row<dx;row+=4)
609 *((unsigned long *)((surf)+(column*lPitch)+(row<<2)))=
610 (*(pD+0)<<16)|(*(pD+1)<<8)|(*(pD+2)&0xff));
618 int LineOffset,SurfOffset;
619 unsigned long * SRCPtr = (unsigned long *)(psxVuw + (y << 10) + x);
620 unsigned long * DSTPtr =
621 ((unsigned long *)surf) + (PreviousPSXDisplay.Range.x0 >> 1);
625 LineOffset = 512 - dx;
626 SurfOffset = (lPitch >> 2) - dx;
628 for(column=0;column<dy;column++)
630 for(row=0;row<dx;row++)
633 lu=GETLE16D(SRCPtr++);
638 ((lu << 10) & 0x7c007c00)|
640 ((lu >> 10) & 0x1f001f);
642 SRCPtr += LineOffset;
643 DSTPtr += SurfOffset;
648 whichImage = 1 - whichImage;
655 - (void)clearBuffer:(BOOL)display
658 //[[self openGLContext] makeCurrentContext];
659 //glClear(GL_COLOR_BUFFER_BIT);
660 //[self loadTextures:NO];
663 // [self setNeedsDisplay:true];
667 - (void)mouseDown:(NSEvent *)theEvent
669 PluginWindowController *controller = [[self window] windowController];
671 static unsigned long lastTime = 0;
677 if (time - lastTime > GetDblTime()) {
679 [[self openGLContext] clearDrawable];
681 [[self openGLContext] setFullScreen];
683 isFullscreen = 1-isFullscreen;