1 /* 2 * Copyright 2012, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Aaron Hill <serac@hillvisions.com> 7 * Alexander von Gluck <kallisti5@unixzen.com> 8 */ 9 10 11 #include "GLifeSaver.h" 12 13 #include <GLView.h> 14 #include <ScreenSaver.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <View.h> 18 19 #include "GLifeGrid.h" 20 #include "GLifeState.h" 21 #include "GLifeConfig.h" 22 #include "GLifeView.h" 23 24 25 // ------------------------------------------------------ 26 // GLifeSaver Class Constructor Definition 27 GLifeSaver::GLifeSaver(BMessage* pbmPrefs, image_id iidImage) 28 : 29 BScreenSaver(pbmPrefs, iidImage) 30 { 31 // Check for preferences 32 if (!pbmPrefs->IsEmpty()) 33 RestoreState(pbmPrefs); 34 35 // Seed random number generator 36 srandom(system_time()); 37 } 38 39 40 // ------------------------------------------------------ 41 // GLifeSaver Class SaveState Definition 42 status_t 43 GLifeSaver::SaveState(BMessage* pbmPrefs) const 44 { 45 return fGLifeState.SaveState(pbmPrefs); 46 } 47 48 49 // ------------------------------------------------------ 50 // GLifeSaver Class RestoreState Definition 51 void 52 GLifeSaver::RestoreState(BMessage* pbmPrefs) 53 { 54 fGLifeState.RestoreState(pbmPrefs); 55 } 56 57 58 // ------------------------------------------------------ 59 // GLifeSaver Class StartConfig Definition 60 void 61 GLifeSaver::StartConfig(BView* pbvView) 62 { 63 // Setup the "config" class 64 GLifeConfig* pglcConfig = new GLifeConfig(pbvView->Bounds(), 65 &fGLifeState); 66 67 pbvView->AddChild(pglcConfig); 68 } 69 70 71 // ------------------------------------------------------ 72 // GLifeSaver Class StartSaver Definition 73 status_t 74 GLifeSaver::StartSaver(BView* pbvView, bool bPreview) 75 { 76 SetTickSize(c_iTickSize); 77 78 fGLifeViewport = new GLifeView(pbvView->Bounds(), 79 "GLifeView", B_FOLLOW_NONE, BGL_RGB | BGL_DEPTH | BGL_DOUBLE, 80 &fGLifeState); 81 82 pbvView->AddChild(fGLifeViewport); 83 84 return B_OK; 85 } 86 87 88 // ------------------------------------------------------ 89 // GLifeSaver Class StopSaver Definition 90 void 91 GLifeSaver::StopSaver(void) 92 { 93 if (fGLifeViewport != NULL) 94 fGLifeViewport->EnableDirectMode(false); 95 } 96 97 98 // ------------------------------------------------------ 99 // GLifeSaver Class DirectConnected Definition 100 void 101 GLifeSaver::DirectConnected(direct_buffer_info* pdbiInfo) 102 { 103 // Enable or disable direct rendering 104 if (fGLifeViewport != NULL) { 105 fGLifeViewport->DirectConnected(pdbiInfo); 106 fGLifeViewport->EnableDirectMode(true); 107 } 108 } 109 110 111 // ------------------------------------------------------ 112 // GLifeSaver Class DirectDraw Definition 113 void 114 GLifeSaver::Draw(BView*, int32 iFrame) 115 { 116 fGLifeViewport->Advance(); 117 } 118 119 120 // ------------------------------------------------------ 121 // Main Instantiation Function 122 extern "C" _EXPORT BScreenSaver* 123 instantiate_screen_saver(BMessage* pbmPrefs, image_id iidImage) 124 { 125 return new GLifeSaver(pbmPrefs, iidImage); 126 } 127