Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:04:53+00:00 2026-06-17T13:04:53+00:00

I am trying to programmatically create a Cocoa window with an OpenGL context for

  • 0

I am trying to programmatically create a Cocoa window with an OpenGL context for an OS X application. I have been unable to find an example online which does not use the Interface Builder for creating a window and OpenGL view.

All I want is for glClear to make my window magenta (0xFF00FF). However, the window remains white.

Here is my project:

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    NSOpenGLContext *openGLContext;
}

@property (assign) NSWindow *window;
@property (assign) NSOpenGLContext *openGLContext;

- (void)draw;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window;
@synthesize openGLContext;

static NSOpenGLPixelFormatAttribute glAttributes[] = {
    0
};

- (void)draw {
    NSLog(@"Drawing...");

    [self.openGLContext makeCurrentContext];

    glClearColor(1, 0, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    [self.openGLContext flushBuffer];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSRect frame = NSMakeRect(0, 0, 200, 200);

    self.window = [[[NSWindow alloc]
        initWithContentRect:frame
        styleMask:NSBorderlessWindowMask
        backing:NSBackingStoreBuffered
        defer:NO] autorelease];
    [self.window makeKeyAndOrderFront:nil];

    NSOpenGLPixelFormat *pixelFormat
        = [[NSOpenGLPixelFormat alloc] initWithAttributes:glAttributes];
    self.openGLContext = [[NSOpenGLContext alloc]
        initWithFormat:pixelFormat shareContext:nil];
    [self.openGLContext setView:[self.window contentView]];

    [NSTimer
        scheduledTimerWithTimeInterval:.1
        target:self
        selector:@selector(draw)
        userInfo:nil
        repeats:YES];
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)_app {
    return YES;
}

@end

main.m

#import "AppDelegate.h"

#import <Cocoa/Cocoa.h>

int main(int argc, char **argv) {
    AppDelegate *appDelegate = [[AppDelegate alloc] init];
    return NSApplicationMain(argc, (const char **) argv);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:04:54+00:00Added an answer on June 17, 2026 at 1:04 pm

    The documentation for -[NSOpenGLContext flushBuffer] says:

    Discussion

    If the receiver is not a double-buffered context, this call does nothing.

    You can cause your context to be double-buffered by including NSOpenGLPFADoubleBuffer in your pixel format attributes. Alternatively, you can call glFlush() instead of -[NSOpenGLContext flushBuffer] and leave your context single-buffered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to programmatically create a window with my own custom OpenGL NSView as
I'm trying to create a UIButton programmatically. I have a button called addCash (which
I'm trying to create a C# application which allows me to extract just the
I'm trying to create a view programmatically. The result that i want to have
i'm trying to create buttons programmatically on my android application depending on how many
Am trying to create a homescreen shortcut programmatically on android. So far I've been
I'm trying to create an NSSearchField programmatically; however, I don't know how to find
I'm trying to programmatically create a folder with Cocoa. I've written an NSString category
Im trying to make my NSViews and windows that I create programmatically have a
I am trying to create UIViewController programmatically for ModalView presentation on the main window

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.